How can I simulate a scrollbar in a DataGrid
<div style=’width:100%; height:200; overflow:auto;’> <asp:DataGrid id=’DataGrid1′ runat=’server’></asp:DataGrid> </div>
How to databind a textbox in a column template that is inside a datagrid
<asp:TextBox runat=’server’ id=’TextBox1′ Text=<%# DataBinder.Eval(Container.DataItem,’theColumName’).ToString() %></asp:TextBox> Same goes true for any other Web Server control
Is there a way to show more than one datafield in a column when using a datagrid?
Yes. Use a TemplateColumn in a DataGrid <asp:DataGrid id=’DataGrid1′ runat=server AutoGenerateColumns=False> <Columns> <asp:TemplateColumn HeaderText=’Field’> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, ‘Field1’).ToString()%> – <%#DataBinder.Eval(Container.DataItem, ‘Field2’).ToString()%> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid>
How to edit data in the DataGrid using a TemplateColumn
<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=False OnCancelCommand =’CancelCmd’ OnEditCommand =’EditCmd’ OnUpdateCommand =’UpdateCmd’ DataKeyField=’Employeeid’ runat=’server’> <Columns> <asp:TemplateColumn HeaderText =’Employee Information’> <ItemTemplate > <asp:label ID=’lblLastName’ Runat=server text=<%#DataBinder.Eval(Container.DataItem, ‘Lastname’)%>></asp:Label> , <asp:label ID=’lblFirstName’ Runat=server text=<%#DataBinder.Eval(Container.DataItem, ‘Firstname’)%>></asp:Label> <br> <asp:label ID=’lblTitle’ Runat=server text=<%#DataBinder.Eval(Container.DataItem, ‘Title’)%>> </ItemTemplate> <EditItemTemplate > <asp:TextBox runat=server ID=’txtLastName’ Text=<%#DataBinder.Eval(Container.DataItem, ‘lastname’)%>></asp:TextBox> <asp:RequiredFieldValidator id=’rqdfldLastName’ Display =’Static’ Runat =server ErrorMessage =’*’ ControlToValidate =’txtLastName’> <asp:TextBox runat=server ID=’txtFirstName’ text=<%#DataBinder.Eval(Container.DataItem, ‘Firstname’)%>></asp:TextBox> <asp:RequiredFieldValidator id=’rqdfldFirstName’ Display =’Static’ Runat =server ErrorMessage =’*’ ControlToValidate =’txtFirstName’> <asp:TextBox runat=server ID=’txtTitle’ Text=<%#DataBinder.Eval(Container.DataItem, ‘title’)%>></asp:TextBox> </EditItemTemplate> </asp:TemplateColumn> <asp:EditCommandColumn ButtonType=’PushButton’ CancelText=’Cancel’ EditText=’Edit’ UpdateText=’Update’></asp:EditCommandColumn> </Columns> </asp:DataGrid> VB.NET Dim sqlStmt As String Dim conString As String Dim cn As SqlConnection Dim cmd As SqlCommand Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’ Put user code to initialize the page here If Not Page.IsPostBack Then BindGrid() End If End Sub ’Page_Load Sub BindGrid() ’Bind the Datagrid with dataSet End Sub ’BindGrid Protected Sub EditCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex) BindGrid() End Sub ’EditCmd Public Sub CancelCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) DataGrid1.EditItemIndex = -1 BindGrid() End Sub ’CancelCmd Protected Sub UpdateCmd(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) If Page.IsValid Then sqlStmt = ‘UPDATE Employees SET LastName = @LastName, FirstName=@FirstName,Title=@Title where EmployeeId = @EmployeeId’ conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’ cn = New SqlConnection(conString) cmd = New SqlCommand(sqlStmt, cn) Dim LastName, FirstName, Title As String LastName = CType(e.Item.FindControl(‘txtLastName’), TextBox).Text FirstName = CType(e.Item.FindControl(‘txtFirstName’), TextBox).Text Title = CType(e.Item.FindControl(‘txtTitle’), TextBox).Text Dim EmployeeId As Integer = CInt(DataGrid1.DataKeys(CInt(e.Item.ItemIndex))) cmd.Parameters.Add(New SqlParameter(‘@LastName’, LastName)) cmd.Parameters.Add(New SqlParameter(‘@FirstName’, FirstName)) cmd.Parameters.Add(New SqlParameter(‘@Title’, Title)) cmd.Parameters.Add(New SqlParameter(‘@EmployeeId’, EmployeeId)) Try cn.Open() cmd.ExecuteNonQuery() DataGrid1.EditItemIndex = -1 Catch ex As Exception Response.Write(ex.Message.ToString()) End Try BindGrid() End If End Sub ’UpdateCmd C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { BindGrid(); } } void BindGrid() { //Bind the DataGrid with dataset } protected void EditCmd(object sender, DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); } public void CancelCmd(object sender, DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = -1; BindGrid(); } string sqlStmt; string conString; SqlConnection cn; SqlCommand cmd; protected void UpdateCmd(object sender, DataGridCommandEventArgs e) { if (Page.IsValid) { sqlStmt = ‘UPDATE Employees SET LastName = @LastName, FirstName=@FirstName,Title=@Title where EmployeeId = @EmployeeId’; conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’; cn = new SqlConnection(conString); cmd = new SqlCommand(sqlStmt, cn); string LastName , FirstName , Title; LastName = ((TextBox )e.Item.FindControl (‘txtLastName’)).Text ; FirstName = ((TextBox )e.Item.FindControl (‘txtFirstName’)).Text ; Title = ((TextBox )e.Item.FindControl (‘txtTitle’)).Text ; int EmployeeId =(int) DataGrid1.DataKeys[(int)e.Item.ItemIndex]; cmd.Parameters.Add(new SqlParameter(‘@LastName’, LastName)); cmd.Parameters.Add(new SqlParameter(‘@FirstName’,FirstName )); cmd.Parameters.Add(new SqlParameter(‘@Title’, Title )); cmd.Parameters.Add(new SqlParameter(‘@EmployeeId’,EmployeeId )); try { cn.Open(); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex = -1; } catch(Exception ex) { Response.Write (ex.Message.ToString () ); } BindGrid(); } }
How to highlight the Column that is sorted in a DataGrid
<asp:DataGrid id=’DataGrid1′ OnItemDataBound =’ItemDB’ AllowSorting=’True’ OnSortCommand=’SortData’ runat=’server’></asp:DataGrid> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then BindDataGrid(‘ProductId’) End If End Sub Protected Sub SortData(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) BindDataGrid(e.SortExpression.ToString()) End Sub Sub BindDataGrid(ByVal sortfield As String) ViewState(‘SortExpression’) = sortfield ’Fill the DataSet Dim dv As DataView = ds.Tables(0).DefaultView dv.Sort = sortfield DataGrid1.DataSource = dv DataGrid1.DataBind() End Sub Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim dv As DataView = DataGrid1.DataSource Dim dc As DataColumnCollection = dv.Table.Columns If e.Item.ItemType = ListItemType.Header Then If ViewState(‘SortExpression’) <> ” Then e.Item.Cells(dc.IndexOf(dc(ViewState(‘SortExpression’)))).BackColor = Color.BlueViolet e.Item.Cells(dc.IndexOf(dc(ViewState(‘SortExpression’)))).ForeColor = Color.AntiqueWhite End If End If End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(! Page.IsPostBack ) { BindDataGrid(‘ProductId’); } } protected void SortData(Object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e ) { BindDataGrid(e.SortExpression.ToString()); } void BindDataGrid(string sortfield) { ViewState[‘SortExpression’] = sortfield; //Fill the dataset DataView dv = ds.Tables[0].DefaultView; dv.Sort = sortfield; DataGrid1.DataSource = dv; DataGrid1.DataBind(); } protected void ItemDB(object sender ,System.Web.UI.WebControls.DataGridItemEventArgs e ) { DataView dv =(DataView) DataGrid1.DataSource; DataColumnCollection dc = dv.Table.Columns; if (e.Item.ItemType == ListItemType.Header ) { if (ViewState[‘SortExpression’].ToString()!= ”) { e.Item.Cells[dc.IndexOf( dc[ViewState[‘SortExpression’].ToString ()])].BackColor = Color.BlueViolet; e.Item.Cells[dc.IndexOf(dc[ViewState[‘SortExpression’].ToString ()])].ForeColor = Color.AntiqueWhite; } } }