DataGrid: 25.1 Why do I get the Columns twice in the datagrid. I am using BoundColumns and TemplateColumns in DataGrid?
Set the AutogenerateColumns= False. By Default it is set to true for a datagrid
25.2 How to Format and display currency with n decimal points in a BoundColumn.
Set the Dataformatstring of the boundcolumn to {0:Nn} where n=> number of decimal points For more details refer Format Function
25.3 How do I specify more than one parameter for my HyperlinkColumn?
25.4 Why am I getting an 'AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID MyDataGrid when AllowPaging is set to true and the selected datasource does not implement ICollection' Error?
You are probably trying to implement paging in a DataGrid while binding the DataGrid with a DataReader. To fix this, instead of using a DataReader use a DataSet
25.5 How do I use a "helper function" to change the column value as Boolean in database to be displayed as Yes/No in Datagrid?
<%#ShowStatus(bool.Parse(DataBinder.Eval(Container.DataItem,"Discontinued").ToString()))%>
VB.NET
protected Function ShowStatus(ByVal someval as boolean) If someval = true Then ShowStatus = "Yes" Else ShowStatus = "No" End If End Function
C#
protected string ShowStatus(bool someval) { if (someval == true) { return "Yes"; } else { return "No"; } }
25.6 How to display only date part in the Datagrid if the Date is of DateTime datatype in the database?
Set the DateFormatString as {0:d}
25.7 How to open a new window with multiple parameters when clicked on a hyperlink in a column in a datagrid?
The column should be defined as a TemplateColumn as follows and the NavigateUrl for that hyperlink can be set as follows to open a new window with parameters varying based on the row in which the hyperlink is present.
DataBinder.Eval(Container.DataItem,"productid").ToString() + "&Supplierid= "+DataBinder.Eval(Container.DataItem,"SupplierID").ToString()+"','my_window','width=300,height=300');my_window.foc % rel="nofollow"> text=<%#DataBinder.Eval(Container.DataItem,"ProductName").ToString() %>>
The above approach would avoid the problem of showing the screen of [Object] on the parent page
25.8 How to display hierarchical data in a DataGrid?
Check out Denis Bauer's custom control HierarGrid
25.9 How do I conditionally set the text color of a cell in my Datagrid based on the cell's/fields's value?
In the ItemDataBound event you can access the Cells() collection of the current Datagrid item (or row) and set it's ForeColor.
VB.NET
protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) If (e.Item.ItemType = ListItemType.Item Or _ e.Item.ItemType = ListItemType.AlternatingItem) Then If e.Item.DataItem(6) > 15 Then e.Item.Cells(6).ForeColor = System.Drawing.Color.Green Else e.Item.Cells(6).ForeColor = System.Drawing.Color.Red End If End If End Sub
C#
protected void ItemDB(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if((e.Item.ItemType ==ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) ) { if (Convert.ToInt16 (e.Item.Cells[6].Text) >15 ) { e.Item.Cells[6].ForeColor = System.Drawing.Color.Green; }
}
else { e.Item.Cells[6].ForeColor = System.Drawing.Color.Red; } }
In Cells[x]/Cells(x) x=> index number to the list of fields in the row.
25.10 When I open my new window using Javascript, all elements appear great, but the original window just displays [Object] why?
Check out How to open a new window with multiple parameters using datagrid? // 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(); }
© 2001-06 Copyright George Shepherd.