Adding RowID when linking a datatable to a datagrid

Posted on 2005-03-07
Medium Priority
Last Modified: 2008-03-10
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
Question by:dracang
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13474845
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


Author Comment

ID: 13475211
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?

Accepted Solution

Qingtong earned 2000 total points
ID: 13477955
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.


Author Comment

ID: 13483971
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

Author Comment

ID: 13484744
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question