How to define Header row for an auto Genarate Gridview

Last post 06-23-2008 11:31 AM by grahamlower. 3 replies.

Sort Posts:

  • How to define Header row for an auto Genarate Gridview

    06-20-2008, 4:13 AM
    • Loading...
    • ashkant
    • Joined on 06-10-2008, 7:32 AM
    • Posts 145

    hi everyone i have a gridview and a linqDataSource which is the source of my gridview data.

    i use some kind of selecting for that --> protected void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)

    {

         

    MyDataContext db = new MyDataContext();

    var show = from A in db.ATable

    select new

    {

    HeaderId = A.Id

    }

    but i have 2 problems. first of all my gridview is auto generate fields checked. but i want to have Readable Headers ( i mean when you progamatically define the headers you can't choose Space or " " for that) how can i define Header row for that

    the second is i dont wanna show my ID column which is the datakey for gridview and when using linq for the datasource it is required.

    Please remember to click “Mark as Answer” on the post that helps you
    Filed under: , ,
  • Re: How to define Header row for an auto Genarate Gridview

    06-22-2008, 1:58 AM
    • Loading...
    • grahamlower
    • Joined on 03-06-2008, 2:12 PM
    • Northern California
    • Posts 179

    Here is some code the demonstrates the two things you are looking to do...

     

    1        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2            Dim d As New DataSet1.DataTable1DataTable
    3            d.AddDataTable1Row("Test1", "RemoveMe")
    4    
    5            Dim d2 As Data.DataTable = d.Copy
    6    
    7            d2.Columns.Remove("RemoveMe")
    8    
    9            With Me.GridView1
    10               .DataSource = d2
    11               .DataBind()
    12               If .HeaderRow IsNot Nothing Then
    13                   .HeaderRow.Cells(d2.Columns.IndexOf("Test1")).Text = "New Title"
    14               End If
    15           End With
    16   
    17       End Sub
    

     To answer you second question, look at lines 5 and 7.  Make a copy of your data table (line 5) and then remove the columns you do not want (line 7).  When the gridview databinds it will not inlcude those columns.

    To answer your second question, look at lines 12 and 13 .  Make sure you have a header row (you may not if you do not have data, line 12) first.  If you do, go to the cell (column) you care about it and change it's text (line 13).  If you want to reference it by name, as I do in the example, use the modifed datatable and searching for ordinal value of the column using the IndexOf method of the columns collection.

    Failure is always an option. Avoid situations where it is the only option.
  • Re: How to define Header row for an auto Genarate Gridview

    06-23-2008, 9:14 AM
    • Loading...
    • ashkant
    • Joined on 06-10-2008, 7:32 AM
    • Posts 145

    thanks

    would you write it in C# too?

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: How to define Header row for an auto Genarate Gridview

    06-23-2008, 11:31 AM
    • Loading...
    • grahamlower
    • Joined on 03-06-2008, 2:12 PM
    • Northern California
    • Posts 179

     Off the top of my head (so there may be some small errors you need to debug):

     

    2            DataSet1.DataTable1DataTable d  = New DataSet1.DataTable1DataTable();
    3 d.AddDataTable1Row("Test1", "RemoveMe");
    4
    5 Data.DataTable d2 = d.Copy();
    6
    7 d2.Columns.Remove("RemoveMe");
    8
    9
    10 Me.GridView1.DataSource = d2;
    11 Me.GridView1.DataBind();
    12 if(Me.GridView1.HeaderRow) {
    13 Me.GridView1.HeaderRow.Cells[d2.Columns.IndexOf("Test1")].Text = "New Title";
    14 }
    15

     

    Cheers.

    Failure is always an option. Avoid situations where it is the only option.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter