• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Adding RowID when linking a datatable to a datagrid

Hi people

Im almost a newbie in .NET, i´ve been learning & developing on it since 2 weeks ago and now i cant solve a problem which I need to solve urgently, hope any of you can help me with this.

I have a DataTable on my codebehind which returns an undefined number of columns, depending on the result of a query. One of these columns (the last one, index "N", column id called "ID") has a value that i want to use as the "id" for each row generated when i bind it to a datagrid. I also want that columnd to be hidden in the datagrid. As a example

If my DataTable Table1 is like this:
col1 col2 col3... colID
a      b      c         id7

when i bind Table1 to Datagrid1 I should see on screen something like this:
col1  col2  col3
a        b      c

and the id for that row should have the next   <tr  id="id7"><td>a</td><td>b</td><td>c</td></tr>
and it would be even better if i could add a style tag for that tr too from the codebehind.

Thanks in advance
  • 3
1 Solution
Ramesh SrinivasTechnical ConsultantCommented:
You can count your columns to get your last column like so:

Dim ColNum = DataTable.Columns.Count

Then to hide...

Datagrid1.Columns(ColNum).Visible = False

dracangAuthor Commented:
Couldnt you just count the number of columns on the datagrid itself like this?

dim DGcolnum = Datagrid1.Columns.Count
Datagrid1.Columns(DGColNum).Visible = False

after you do the databind?
in your datagrid event, put the line as

ItemDataBoundItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles XXXXX.ItemDataBound

e.Item.cells(e.item.cells.count-1).visible = false

Since you are auto generating the coulums and always know the last one is for ID and not displayed.

Hope it may help.

dracangAuthor Commented:
Ey QingTong, your solution works fine to hide the column  (i couldn make Saleek´s solution to work), thank u very much!. Anyway, this is just half of the job done, any clue about inserting the "id" in each datagrid row?, and adding also a style tag onto each of that rows?

any quick answer, please?

thanks again
dracangAuthor Commented:
A guy here at my office just gave me a solution which together with yours fixed the problem.

these lines solved the problem:

    Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        e.Item.Cells(e.Item.Cells.Count - 1).Visible = False
        e.Item.Attributes.Add("id", e.Item.Cells(e.Item.Cells.Count - 1).Text)
    End Sub

Thankyou very much again

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now