Data Grid Girl!  




  Home
  FAQ
  Articles
  Books
  Custom
  Columns
  Consulting
  Blog
  Talks
  Gallery
  Contact!


Frequently Asked Questions

Question: How do I use a "helper function" to alter the output within my Datagrid based on a value from the DataSource?

Answer: Here is a sample use of a helper function (VB.Net)

<asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Status">
  <ItemTemplate>
    <%#ShowStatus(Container.DataItem("online"))%>
  </ItemTemplate>
</asp:TemplateColumn>

Function ShowStatus(val)
  If val = 0 Then
    ShowStatus = "Offline"
  Else
    ShowStatus = "Online"
  End If
End Function

Question: How do I change the width of the Textboxes created in edit-mode of my Datagrid?

Updated! Answer: Here's an example of how to set the width of an edit Textbox in the EditItemTemplate of your TemplateColumn:

<asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Sample Column">
  <ItemTemplate>
    <%#Container.DataItem("AColumnName")%>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:Textbox runat="server" width="600" maxlength="600"/>
  </EditItemTemplate>
</asp:TemplateColumn>

And for a BoundColumn, you can modify the width just before the Datagrid is rendered:

Private Sub DataGrid1_PreRender(s As Object, e As EventArgs)
If DataGrid1.EditItemIndex > -1 Then
  Dim tbx As TextBox
  tbx = CType(DataGrid1.Items(DataGrid1.EditItemIndex).Cells(0).Controls(0), TextBox)
  tbx.Width = Unit.Parse("1cm")
End If
End Sub

Question: How do I hide a column in my Datagrid if AutoGenerateColumns is set to True?

Answer: AutoGenerated columns do not appear in the Datagrid's Columns() collection, and so the usual method of hiding a Datagrid column will fail:

'Will NOT work for AutoGenerated columns:
Datagrid1.Columns(1).Visible = False

So the place to handle this is in the ItemDataBound event of the Datagrid:

<asp:DataGrid id="Datagrid1" runat="server" AutoGenerateColumns="True" OnItemDataBound="Datagrid1_OnItemDataBound"/>

Private Sub DataGrid1_ItemDataBound(s As Object, e As DatagridItemEventArgs)
  e.Item.Cells(1).Visible = False
End Sub

Question: When I try to do an Update from my Datagrid, I keep getting the old/original values. Why?

Answer: This is caused by calling DataBind() on the grid before retrieving the updated values. The DataBind() overwrites the updates with the original values. Usually caused by having the initial DataBind() of the grid called on every Page_Load. Solution: call DataBind() only the first time the page is loaded:

Sub Page_Load(s As Object, e As EventArgs)
  If Not IsPostBack Then BindGrid()
End Sub

Question: How do I conditionally set the backcolor of a cell in my Datagrid based on a value from the database? (conditional formatting)

Answer: Handle this in the ItemDataBound event, where you can access the DataItem as it is bound to the Datagrid, and the Cells() collection of the current Datagrid item.

Sub Datagrid1_ItemDataBound(source As Object, e As DataGridItemEventArgs)
  If (e.Item.ItemType = ListItemType.Item Or _
    e.Item.ItemType = ListItemType.AlternatingItem) Then
    If Convert.ToDateTime(e.Item.Cells(1).Text) < DateTime.Today Then _
    e.Item.Cells(1).BackColor = System.Drawing.Color.FromName("#ffccff")
    If e.Item.DataItem("UserID") = 590 Then _
    e.Item.Cells(2).BackColor = System.Drawing.Color.DeepPink
  End If
End Sub

Question: How do I specify more than one parameter for my HyperlinkColumn?

Answer: The HyperlinkColumn's DataNavigateUrlFormatString only supports one parameter. If you need more than one, switch to a TemplateColumn with an <asp:Hyperlink>.
Note: You should always encode values passed into querystring to protect against spaces and special characters.

<asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Sample Column">
  <ItemTemplate>
    <asp:Hyperlink runat="server" Text='<%#Container.DataItem("TextVal")%>' NavigateUrl='<%# "page.aspx?Param1=" & Server.UrlEncode(Container.DataItem("Val1")) & "&Param2=" & Server.UrlEncode(Container.DataItem("Val2"))%>'/>
  </ItemTemplate>
</asp:TemplateColumn>
</Columns>

Question: How can I make the content of one of my Datagrid cells span more than one line? Or, how do I display more than one field of data in the same cell?

Answer: Use a TemplateColumn, and inside the ItemTemplate (or EditItemTemplate), you can use <br> tags or any other formatting you might want.

<asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Sample Column">
  <ItemTemplate>
<%#Container.DataItem("Field1")%>
<br>
<%#Container.DataItem("Field2")%>
  </ItemTemplate>
</asp:TemplateColumn>
</Columns>


Hosted by MaximumASP