DataGrid Columns

I have to write this over again AGAIN. GRR. CLARIFICATION: I was angry at the WebLog app for losing my post again and having to start over. It's been doing that to me a lot lately.

OK, I'm going to beat DataGridGirl to the punch on this one. Samer asked about how to access an item ID from a hidden ID column. I use a really simple solution. Below is the code from one of my apps, in it's entirety. It has some DAL code in it, just disregard... you should be able to get the gist of it.

Sub DataGrid1_Delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
    Dim iid As Integer = Convert.ToInt32(e.Item.Cells(0).Text)
    Dim artwork As New KinkadeNet.ArtworkDB
    Dim Result As Integer = DataGrid1.Items.Count Mod DataGrid1.PageSize
    If Result = 1 Then
        DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 2)
    End If
    artwork.DeleteDealerInventory(iid)
    Dim loc As Integer = 1
    DataGrid1.DataSource = artwork.GetLocationInventory(loc)
    DataGrid1.DataBind()
End Sub

The first line shows how I grab the hidden id, the rest shows how I use it. That's all you have to do. I hope that helps. BTW, this code is also covered in my first two builder.com articles. Go check them out... I've got some good tidbits in there.

UPDATE: In the 2nd line, the part "e.Item.Cells(0).Text", (0) is the index of the column with your hidden ID. Remember column in the DataGrid are zero-based. For consistency, you should always make hidden ID column the first one in the Grid.

No Comments