Can we change the heading of a datagrid?
-
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:
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
-
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
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?
-
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?
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
-
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
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)
-
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)
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
-
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)
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?
-
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?
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:
-
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:
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....
-
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....
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.:)
-
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.:)