Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Can we change the heading of a datagrid?

Can we change the heading of a datagrid?

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lavanya_satheesh
    wrote on last edited by
    #1

    when u bind a datagrid to a database table the column headings that are displayed are the field name of the table. can we change that? if someone out there can help please do.:confused:

    B 1 Reply Last reply
    0
    • L lavanya_satheesh

      when u bind a datagrid to a database table the column headings that are displayed are the field name of the table. can we change that? if someone out there can help please do.:confused:

      B Offline
      B Offline
      Briga
      wrote on last edited by
      #2

      There are lot of answers to this already posted question. You can easily search this forum. Last one is this one: http://www.codeproject.com/script/comments/forums.asp?forumid=1646&fr=51#xx1290813xx

      L 1 Reply Last reply
      0
      • B Briga

        There are lot of answers to this already posted question. You can easily search this forum. Last one is this one: http://www.codeproject.com/script/comments/forums.asp?forumid=1646&fr=51#xx1290813xx

        L Offline
        L Offline
        lavanya_satheesh
        wrote on last edited by
        #3

        Thanks for the reply. But my problem is this: I have a datagrid. i want to create the connection, bind the data and then change the header at run-time not at design time. If i am creating the connection and binding the data to the grid at design time then i am able to change the header by writing code on the code behind page. but when i am trying to create the connection and also bind the grid at runtime it is not working. can u help me with this?

        B 1 Reply Last reply
        0
        • L lavanya_satheesh

          Thanks for the reply. But my problem is this: I have a datagrid. i want to create the connection, bind the data and then change the header at run-time not at design time. If i am creating the connection and binding the data to the grid at design time then i am able to change the header by writing code on the code behind page. but when i am trying to create the connection and also bind the grid at runtime it is not working. can u help me with this?

          B Offline
          B Offline
          Briga
          wrote on last edited by
          #4

          What I was referring works perfectly also in your case. Have you followed the example in the official help? If you don't state exactly what you do, or post some code, it's rather complex to come up with an answer. Other examples are also available on your help under the DataGridColumnStyle class documentation: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_DataGridColumnStyle.htm

          L 1 Reply Last reply
          0
          • B Briga

            What I was referring works perfectly also in your case. Have you followed the example in the official help? If you don't state exactly what you do, or post some code, it's rather complex to come up with an answer. Other examples are also available on your help under the DataGridColumnStyle class documentation: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_DataGridColumnStyle.htm

            L Offline
            L Offline
            lavanya_satheesh
            wrote on last edited by
            #5

            i am sorry if i offended u in some way. i was unable to open the link u specified. c my prob is this: i have a datagrid and i have created a connection at design time and populated the grid with data using the foll code. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.SqlDataAdapter1.Fill(DataSet11) then i wrote the code below to change the headers: 'Step 1: Create a DataGridTableStyle & ' set mappingname to table. Dim tableStyle As New DataGridTableStyle tableStyle.MappingName = "BillProductDet" 'Step 2: Create DataGridColumnStyle for each col ' we want to see in the grid and in the ' order that we want to see them. 'Step 2: BillNo Dim column As New DataGridTextBoxColumn column.MappingName = "BillNo" column.HeaderText = "ID" column.Width = 30 tableStyle.GridColumnStyles.Add(column) 'MfgDate column = New DataGridTextBoxColumn column.MappingName = "MfgDate" column.HeaderText = "M.Date" tableStyle.GridColumnStyles.Add(column) 'Step 2: UnitPrice column = New DataGridTextBoxColumn column.MappingName = "UnitPrice" column.HeaderText = "Unit Price" tableStyle.GridColumnStyles.Add(column) 'Step 3: Add the tablestyle to the datagrid Me.DataGrid1.TableStyles.Add(tableStyle) End Sub this is working but when i am tryting to create the connection at run time using the following code it is not working: Dim sqlcon2 As New SqlConnection(constr) Dim sqladp2 As New SqlDataAdapter Dim cmdstr1 As New SqlCommand Dim strq1 As String sqlcon2.Open() strq1 = "select * from Products" Try sqladp2.SelectCommand = cmdstr1 sqladp2.SelectCommand.CommandText = strq1 sqladp2.SelectCommand.Connection = sqlcon2 dsdata2.Clear() sqladp2.Fill(dsdata2, "SearchResult") If dsdata2.Tables("SearchResult").Rows.Count > 0 Then dgrid.DataSource = dsdata2 dgrid.DataMember = "SearchResult" dgrid.Refresh() End If cmdstr1.Dispose() sqlcon2.Close() sqlcon2.Dispose() sqladp2.Dispose() Catch empexp As SqlException MessageBox.Show(empexp.Message)

            V B 2 Replies Last reply
            0
            • L lavanya_satheesh

              i am sorry if i offended u in some way. i was unable to open the link u specified. c my prob is this: i have a datagrid and i have created a connection at design time and populated the grid with data using the foll code. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.SqlDataAdapter1.Fill(DataSet11) then i wrote the code below to change the headers: 'Step 1: Create a DataGridTableStyle & ' set mappingname to table. Dim tableStyle As New DataGridTableStyle tableStyle.MappingName = "BillProductDet" 'Step 2: Create DataGridColumnStyle for each col ' we want to see in the grid and in the ' order that we want to see them. 'Step 2: BillNo Dim column As New DataGridTextBoxColumn column.MappingName = "BillNo" column.HeaderText = "ID" column.Width = 30 tableStyle.GridColumnStyles.Add(column) 'MfgDate column = New DataGridTextBoxColumn column.MappingName = "MfgDate" column.HeaderText = "M.Date" tableStyle.GridColumnStyles.Add(column) 'Step 2: UnitPrice column = New DataGridTextBoxColumn column.MappingName = "UnitPrice" column.HeaderText = "Unit Price" tableStyle.GridColumnStyles.Add(column) 'Step 3: Add the tablestyle to the datagrid Me.DataGrid1.TableStyles.Add(tableStyle) End Sub this is working but when i am tryting to create the connection at run time using the following code it is not working: Dim sqlcon2 As New SqlConnection(constr) Dim sqladp2 As New SqlDataAdapter Dim cmdstr1 As New SqlCommand Dim strq1 As String sqlcon2.Open() strq1 = "select * from Products" Try sqladp2.SelectCommand = cmdstr1 sqladp2.SelectCommand.CommandText = strq1 sqladp2.SelectCommand.Connection = sqlcon2 dsdata2.Clear() sqladp2.Fill(dsdata2, "SearchResult") If dsdata2.Tables("SearchResult").Rows.Count > 0 Then dgrid.DataSource = dsdata2 dgrid.DataMember = "SearchResult" dgrid.Refresh() End If cmdstr1.Dispose() sqlcon2.Close() sqlcon2.Dispose() sqladp2.Dispose() Catch empexp As SqlException MessageBox.Show(empexp.Message)

              V Offline
              V Offline
              Vimal Raj
              wrote on last edited by
              #6

              Hi, I am not sure if this is what you are lookin for. You can change the sql query like this Select BillNo as ID,MfgDate as MDate,unitprice as 'Unit Price' from BillProduct. This will give the header texts as you require.Otherwise there is no text property exposed for a rowheader cell. But you can handle the Paint event and draw header text yourself. Hopes this meet ur needs Vimal Raj

              1 Reply Last reply
              0
              • L lavanya_satheesh

                i am sorry if i offended u in some way. i was unable to open the link u specified. c my prob is this: i have a datagrid and i have created a connection at design time and populated the grid with data using the foll code. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.SqlDataAdapter1.Fill(DataSet11) then i wrote the code below to change the headers: 'Step 1: Create a DataGridTableStyle & ' set mappingname to table. Dim tableStyle As New DataGridTableStyle tableStyle.MappingName = "BillProductDet" 'Step 2: Create DataGridColumnStyle for each col ' we want to see in the grid and in the ' order that we want to see them. 'Step 2: BillNo Dim column As New DataGridTextBoxColumn column.MappingName = "BillNo" column.HeaderText = "ID" column.Width = 30 tableStyle.GridColumnStyles.Add(column) 'MfgDate column = New DataGridTextBoxColumn column.MappingName = "MfgDate" column.HeaderText = "M.Date" tableStyle.GridColumnStyles.Add(column) 'Step 2: UnitPrice column = New DataGridTextBoxColumn column.MappingName = "UnitPrice" column.HeaderText = "Unit Price" tableStyle.GridColumnStyles.Add(column) 'Step 3: Add the tablestyle to the datagrid Me.DataGrid1.TableStyles.Add(tableStyle) End Sub this is working but when i am tryting to create the connection at run time using the following code it is not working: Dim sqlcon2 As New SqlConnection(constr) Dim sqladp2 As New SqlDataAdapter Dim cmdstr1 As New SqlCommand Dim strq1 As String sqlcon2.Open() strq1 = "select * from Products" Try sqladp2.SelectCommand = cmdstr1 sqladp2.SelectCommand.CommandText = strq1 sqladp2.SelectCommand.Connection = sqlcon2 dsdata2.Clear() sqladp2.Fill(dsdata2, "SearchResult") If dsdata2.Tables("SearchResult").Rows.Count > 0 Then dgrid.DataSource = dsdata2 dgrid.DataMember = "SearchResult" dgrid.Refresh() End If cmdstr1.Dispose() sqlcon2.Close() sqlcon2.Dispose() sqladp2.Dispose() Catch empexp As SqlException MessageBox.Show(empexp.Message)

                B Offline
                B Offline
                Briga
                wrote on last edited by
                #7

                lavanya_satheesh wrote:

                i am sorry if i offended u in some way. i was unable to open the link u specified.

                You haven't offended me! :) The link is rather weird. I guess you have to copy and paste it by yourself in the MSDN Help (don't know how it works that way). If you type it in a browser you'll go nowhere. Now let's move onto the problem. If you're exposing a datagrid with a dataset as a source you should be able to apply table styles and thus custom columns regardless of the way you populate the dataset. I checked your code and seems ok although I'm missing how you apply the style in the latter case. When you say "it doesn't work" what do you mean exactly? You get an error? It doesn't compile, or simply you see the datagrid with the unchanged column header? If your answer is the last one then, probably, the problem should be related to the mapping of the TableStyle or the Column itself. Have you tried to add a n empty test column just to see if it works?

                L 1 Reply Last reply
                0
                • B Briga

                  lavanya_satheesh wrote:

                  i am sorry if i offended u in some way. i was unable to open the link u specified.

                  You haven't offended me! :) The link is rather weird. I guess you have to copy and paste it by yourself in the MSDN Help (don't know how it works that way). If you type it in a browser you'll go nowhere. Now let's move onto the problem. If you're exposing a datagrid with a dataset as a source you should be able to apply table styles and thus custom columns regardless of the way you populate the dataset. I checked your code and seems ok although I'm missing how you apply the style in the latter case. When you say "it doesn't work" what do you mean exactly? You get an error? It doesn't compile, or simply you see the datagrid with the unchanged column header? If your answer is the last one then, probably, the problem should be related to the mapping of the TableStyle or the Column itself. Have you tried to add a n empty test column just to see if it works?

                  L Offline
                  L Offline
                  lavanya_satheesh
                  wrote on last edited by
                  #8

                  hi, thanks for the reply with the smile.:-D actually when i say it doesnt work i meant the data comes with unchanged headers. i have added many test columns to c whether it works. like vimal420 posted i am able to change the headers using the query also but i am unable to do so using the code i specified. if the mappings are correct when the dataset is populated at design time then it should b correct when i give the same code at runtime also right? i dont understand what is wrong:confused:

                  B 1 Reply Last reply
                  0
                  • L lavanya_satheesh

                    hi, thanks for the reply with the smile.:-D actually when i say it doesnt work i meant the data comes with unchanged headers. i have added many test columns to c whether it works. like vimal420 posted i am able to change the headers using the query also but i am unable to do so using the code i specified. if the mappings are correct when the dataset is populated at design time then it should b correct when i give the same code at runtime also right? i dont understand what is wrong:confused:

                    B Offline
                    B Offline
                    Briga
                    wrote on last edited by
                    #9

                    That's sound weird to me (and maybe not 100% clear). The only thing is that a dataset cannot be populated at design time. It's always populated at run time, normally through a fill. So I wonder if you mean that the difference is that in one case you have a ...(forgot the name)... dataset and in the other not. The word I'm missing here is the common terminology to state that a dataset has (or not) a specific schema associated and therefore columns have specific names. Because if this is the case than there's a difference we can spot. It's easy to tell with intellisense since in the former case you can type dataset.tablename.columname while in the latter you have to refer them as dataset.table(x).item(y) (if i recall it correctly since I use only the first type). If this is the case then mapping it's definetly the point since when you populate the dataset you fill in the data but not the schema. If not than I don't have a prompt answer. What I suggest you is to go through some tries like: - Comment the code that populate the dataset, leave it empty, and see if something changes - Add test columns to see if they come up (or you're missing the whole tablestyle) - Try customizing different column properties....

                    L 1 Reply Last reply
                    0
                    • B Briga

                      That's sound weird to me (and maybe not 100% clear). The only thing is that a dataset cannot be populated at design time. It's always populated at run time, normally through a fill. So I wonder if you mean that the difference is that in one case you have a ...(forgot the name)... dataset and in the other not. The word I'm missing here is the common terminology to state that a dataset has (or not) a specific schema associated and therefore columns have specific names. Because if this is the case than there's a difference we can spot. It's easy to tell with intellisense since in the former case you can type dataset.tablename.columname while in the latter you have to refer them as dataset.table(x).item(y) (if i recall it correctly since I use only the first type). If this is the case then mapping it's definetly the point since when you populate the dataset you fill in the data but not the schema. If not than I don't have a prompt answer. What I suggest you is to go through some tries like: - Comment the code that populate the dataset, leave it empty, and see if something changes - Add test columns to see if they come up (or you're missing the whole tablestyle) - Try customizing different column properties....

                      L Offline
                      L Offline
                      lavanya_satheesh
                      wrote on last edited by
                      #10

                      hi, i am sorry if i am still not able to make myself clear to u. in both the cased i am having a dataset. in the first case the connection,adapter and dataset are created at design time and the dataset is populated with fill. in the second case connection, adapter and dataset are created and the run time and the dataset is populated with the code: sqladp2.Fill(dsdataset2, "SearchResult") and then the data is displayed in the grid using the code: If dsdataset2.Tables("SearchResult").Rows.Count > 0 Then DataGrid1.DataSource = dsdataset2 DataGrid1.DataMember = "SearchResult" DataGrid1.Refresh() End If if u find any prob with this let me know. otherwise it is ok. i actually solved the problem by changing the sql query. i will try ur suggestions and let you know the result. thanks for taking the time friend, thanks a lot.:)

                      B 1 Reply Last reply
                      0
                      • L lavanya_satheesh

                        hi, i am sorry if i am still not able to make myself clear to u. in both the cased i am having a dataset. in the first case the connection,adapter and dataset are created at design time and the dataset is populated with fill. in the second case connection, adapter and dataset are created and the run time and the dataset is populated with the code: sqladp2.Fill(dsdataset2, "SearchResult") and then the data is displayed in the grid using the code: If dsdataset2.Tables("SearchResult").Rows.Count > 0 Then DataGrid1.DataSource = dsdataset2 DataGrid1.DataMember = "SearchResult" DataGrid1.Refresh() End If if u find any prob with this let me know. otherwise it is ok. i actually solved the problem by changing the sql query. i will try ur suggestions and let you know the result. thanks for taking the time friend, thanks a lot.:)

                        B Offline
                        B Offline
                        Briga
                        wrote on last edited by
                        #11

                        Your code seems correct to me (but when do you apply the tablestle? Before or after that if/endif block?). I'm sorry not to be more helpful ... but you know remotely is even more difficult.... :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups