Why do I get error message ‘NewPageIndex is not a member of ‘System.EventArgs’.’

You probably declared the event handler for OnPageIndexChanged event incorrectly. Make sure that the event handler looks like the following. <asp:DataGrid id=’DataGrid1′ runat=’server’… OnPageIndexChanged='<EventName>’…/> VB.NET protected Sub <Eventname>(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) ’… end sub C# protected void <EventName>(object sender , System.Web.UI.WebControls.DataGridPageChangedEventArgs) { //… }

How to create a Main Header in DataGrid along with the DataGrid Column Headers

You have to manually add a row to the table generated by the datagrid as follows. <asp:DataGrid id=’DataGrid1′ OnPreRender =’dgPreRender’ runat=’server’></asp:DataGrid> VB.NET protected Sub dgPreRender(ByVal sender As Object, ByVal e As System.EventArgs) Dim dgItem As New DataGridItem(0, 0, ListItemType.Header) Dim tbCell As New TableCell tbCell.ColumnSpan = 3 ’Set it to the colspan that you want tbCell.Text = ‘Category Information’ tbCell.Attributes.Add(‘style’, ‘text-align:center’) dgItem.Cells.Add(tbCell) DataGrid1.Controls(0).Controls.AddAt(0, dgItem) End Sub C# protected void dgPreRender(object sender, System.EventArgs e ) { DataGridItem dgItem = new DataGridItem (0, 0, ListItemType.Header); TableCell tbCell = new TableCell(); tbCell.ColumnSpan = 3;// Set it to the colspan that you want tbCell.Text = ‘Category Information’; tbCell.Attributes.Add(‘style’, ‘text-align:center’); dgItem.Cells.Add(tbCell); DataGrid1.Controls[0].Controls.AddAt(0, dgItem); }

How to export DataGrid data to excel

Use namespace System.IO 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 ’Bind the DataGrid to DataSet DataGridToExcel(DataGrid1, Response) End Sub Protected Sub DataGridToExcel(ByVal dGridExport As DataGrid, ByVal httpResp As HttpResponse) httpResp.Clear() httpResp.Charset = ” httpResp.ContentType = ‘application/vnd.ms-excel’ Dim stringWrite As New StringWriter Dim htmlWrite As New HtmlTextWriter(stringWrite) Dim dGrid As New DataGrid dGrid = dGridExport dGrid.HeaderStyle.Font.Bold = True dGrid.DataBind() dGrid.RenderControl(htmlWrite) httpResp.Write(stringWrite.ToString) httpResp.End() End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here //Bind the DataGrid to DataSet DataGridToExcel (DataGrid1, Response); } protected void DataGridToExcel(DataGrid dGridExport , HttpResponse httpResp) { httpResp.Clear(); httpResp.Charset = ”; httpResp.ContentType = ‘application/vnd.ms-excel’; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); DataGrid dGrid = new DataGrid(); dGrid = dGridExport; dGrid.HeaderStyle.Font.Bold = true; dGrid.DataBind(); dGrid.RenderControl(htmlWrite); httpResp.Write(stringWrite.ToString()); httpResp.End(); }

How to use the DataFormatString to format DataGrid data dynamically

In the ItemDataBound Event of DataGrid write following code. VB.NET Dim ds As DataSet = CType(DataGrid1.DataSource, DataSet) Dim dv As DataView = ds.Tables(0).DefaultView Dim dcCol As DataColumnCollection = dv.Table.Columns If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then e.Item.Cells(dcCol.IndexOf(dcCol(‘UnitPrice’))).Text = DataBinder.Eval(e.Item.DataItem, ‘UnitPrice’, ‘{0:c}’) End If C# DataSet ds = (DataSet)DataGrid1.DataSource ; DataView dv = ds.Tables[0].DefaultView ; DataColumnCollection dcCol = dv.Table.Columns ; if ((e.Item.ItemType == ListItemType.Item )||( e.Item.ItemType == ListItemType.AlternatingItem )) { e.Item.Cells[dcCol.IndexOf (dcCol [‘UnitPrice’])].Text = DataBinder.Eval(e.Item.DataItem, ‘UnitPrice’, ‘{0:c}’); }

Why do I get the error message ‘ CS1502: The best overloaded method match for ‘xxx(string)’ has some invalid arguments ‘ when I use Helper function in DataGrid

The function xxx() is expecting a string, but DataBinder.Eval() returns an object. So you must cast the result of DataBinder.Eval() to a string (or, in this case, just use .ToString()). i.e, in your template do: <%#xxx(DataBinder.Eval(Container.DataItem, ‘field_name’).ToString()) %> VB.NET <%#xxx(DataBinder.Eval (CStr(DataBinder.Eval(Container.DataItem, ‘field_name’)) )%> C# <%# xxx((string) DataBinder.Eval(Container.DataItem, ‘field_name’))%> Either of these should work.