Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have used BindingSource to bind DataGrid in my window 6.5 application. But problem is that I want to change the text of a column from Int to string while grid is binding.

If you look at below code I am binding DataGrid with Designation which value is 502 but I need to display "SE" instead, I cant take that value in my Candidate object so I need to modify DataGrid at runtime. I dont know how can I achieve this. Please help . Thanks.
C#
cand.Add(new Candidate { ID = 10, Name = "Andrew", Designation =501 ,DCode="SSE" });
          cand.Add(new Candidate { ID = 11, Name = "Peter", Designation = 502 , DCode="SE"});
          DataGridTableStyle myDataGridTableStyle = new DataGridTableStyle();
          myDataGridTableStyle.MappingName = "Candidate";
          DataGridTextBoxColumn colA = new DataGridTextBoxColumn();
          colA.MappingName = "Name";
          colA.HeaderText = "Field A";
          colA.Width = 50;
          DataGridTextBoxColumn colB = new DataGridTextBoxColumn();
          colB.MappingName = "Designation";
          colB.HeaderText = "Position Holding";
          colB.Width = 100;
          //dataGrid1.CurrentCell.ColumnNumber.
          DataGridTextBoxColumn colC = new DataGridTextBoxColumn();
          colC.MappingName = "DCode";
          colC.HeaderText = "Code Position";
          colC.Width = 50;

          myDataGridTableStyle.GridColumnStyles.Add(colA);
          myDataGridTableStyle.GridColumnStyles.Add(colB);
          myDataGridTableStyle.GridColumnStyles.Add(colC);

          myBindingSource.DataSource = cand.ToBindingList();
          myDataGridTableStyle.MappingName = myBindingSource.GetListName(null);
          dataGrid1.TableStyles.Clear(); // Recommended on MSDN in the code examples.
          dataGrid1.TableStyles.Add(myDataGridTableStyle);
          dataGrid1.DataSource = myBindingSource;



Thanks in advance.
Vivek
Posted
Comments
The14thNoah 19-Feb-14 20:03pm    
you need to diplsy DCode column and dont show the Designation column right?
try to make the colB visible to false so that your DataGrid show only colA and colC

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900