This is a migrated thread and some comments may be shown as answers.

Bind to custom columns

5 Answers 234 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roy
Top achievements
Rank 1
Roy asked on 16 Feb 2014, 11:27 AM
hi,
I'm trying to bind data table to gridview without any success.

My gridview contains a few columns like Date (DateTimeColumn),  Name (TextBoxColumn),  User: (ComboColumn)...

when i read my data from server to dataTable its contains (iDate, iName, iUser, and a few more columns,  more than the gridview contains)  
and as you can see the names of columns are differents.

In that case,   how can i bind the dataTable to gridview??   

Best regards,
Roy

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 18 Feb 2014, 12:37 PM
Hello Roy,

Thank you for writing. 

In this case you need to set the FieldName of the columns you added to the real column name in your DataTable. This way, the added column will get its data from the specified field.

I hope this helps. 

Regards,
Stefan
Telerik
0
Roy
Top achievements
Rank 1
answered on 19 Feb 2014, 07:57 AM
hi Stefan,
Thanks for your quick reply.

I changed the dataTable fields names exactly as in GridView  (even removed the rest fields from dataTable)
and the result is the same,
its bind the correct no of rows but the cells are empty....

i tried both:
1) gvLogView.DataSource = dt;

2) BindingSource bs = new BindingSource();
  bs.DataSource = dt;
  gvLogView.DataSource = bs;
0
Roy
Top achievements
Rank 1
answered on 19 Feb 2014, 08:13 AM
my mistake,   its created duplicate columns so i set the gvLogView.MasterTemplate.AutoGenerateColumns = false;
but it still shows empty cells.  
( Just for clarify,  i checked the dataTable and its contain data! )
0
Stefan
Telerik team
answered on 19 Feb 2014, 09:30 AM
Hello Roy,

Here is a small sample demonstrating how to achieve the desired binding when your DataTable column has different names:
public Form1()
{
    InitializeComponent();
 
    DataTable dataFromServer = new DataTable();
    dataFromServer.Columns.Add("iDate", typeof(DateTime));
    dataFromServer.Columns.Add("iName", typeof(string));
    dataFromServer.Columns.Add("iUser", typeof(string));
 
    dataFromServer.Rows.Add(DateTime.Now.AddDays(1), "Name 1", "User 1");
    dataFromServer.Rows.Add(DateTime.Now.AddDays(2), "Name 3", "User 2");
    dataFromServer.Rows.Add(DateTime.Now.AddDays(3), "Name 4", "User 3");
    dataFromServer.Rows.Add(DateTime.Now.AddDays(4), "Name 5", "User 4");
 
    GridViewDateTimeColumn dateTimeCol = new GridViewDateTimeColumn();
    dateTimeCol.Name = "Date";
    dateTimeCol.HeaderText = "Date";
    dateTimeCol.FieldName = "iDate";
    radGridView1.Columns.Add(dateTimeCol);
 
    GridViewTextBoxColumn textCol = new GridViewTextBoxColumn();
    textCol.Name = "Name";
    textCol.HeaderText = "Name";
    textCol.FieldName = "iName";
    radGridView1.Columns.Add(textCol);
 
    GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn();
    comboCol.Name = "User";
    comboCol.HeaderText = "User";
    comboCol.FieldName = "iUser";
    radGridView1.Columns.Add(comboCol);
 
    radGridView1.AutoGenerateColumns = false;
    radGridView1.DataSource = dataFromServer;
}

I hope this helps. 

Regards,
Stefan
Telerik
0
Roy
Top achievements
Rank 1
answered on 19 Feb 2014, 11:35 AM
hi Stefan,
Thank you so much,  it works like a magic    :-)
Tags
GridView
Asked by
Roy
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Roy
Top achievements
Rank 1
Share this question
or