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

Import Excel to corresponding defined columns of gridview

1 Answer 294 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sree
Top achievements
Rank 1
Sree asked on 04 Jan 2013, 08:55 AM
Hello Team

I have a requirement where i have radgridview which is having 5 predefined columns , i need to upload data into these fields from excel

i have used the following code to populate the columns from excel, but they are adding next to the predefined columns, i need to map the grid columns to excel columns, how can i achieve this




excel



i need to populate the above values  (with out headertext ) from excel  into the corresponding columns of grid,





from the above code i am adding auto generate columns to grid, but i want these data to be populated in the corresponding columns  any help????

1 Answer, 1 is accepted

Sort by
0
Accepted
Julian Benkov
Telerik team
answered on 08 Jan 2013, 05:19 PM
Hello Lakshmi,

To reuse the existing RadGridView columns with the DataSource, you should map the ColumnName from the DataTable object to RadGridView Columns FieldName property. Here is a sample based on your code snippet:

DataTable table = data.Tables[0];
 
gridView.ColumnCount = 5;
gridView.AutoGenerateColumns = false;
int index = 0;
foreach (DataColumn col in table.Columns)
{
    this.gridView.Columns[index].FieldName = col.ColumnName;
    index++;
}
gridView.DataSource = table;

Please keep in mind that the DataTable.Columns Count must be equal to the count of RadGridView columns.

I hope this helps.

All the best,
Julian Benkov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
Sree
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or