0

I have a gridview which has 4 columns in total now the first column name i am importing from an excel sheet which is named as "FileName".the gridview is taking columnheader as F1 instead of filename.i need that f1 to be replaced with my column header name.

so far I have tried

dataGridView1.Columns[0].Name = "Filename";
dtExcel.Columns.Add("Filename");
dataGridView1.Columns[0].HeaderText = "Filename"

by this, all new column of the filename is getting added but that F1 is not getting replaced.

Help Please.

2 Answers 2

1

Daclare a GridView for example: GridView1 Now before uploading your excel, make the column Name the one you want with below code

GridView1.Columns[0].HeaderText = "SampleCoumnName"; //1st Column
GridView1.Columns[1].HeaderText = "SampleCoumnName2";//2nd Column

Now upload the Excel file in the above Grid "GridView1". Do your work and export from same Grid.

Update2:

DataRow row = datatable.Rows[0];
dt.Rows.Remove(row);
GridView1.DataSource = dt;

Above Snippet believes, you are adding excel values into DataTable. If this, Get the 0 row. Remove that row and then upload into your Griview with the desired column name you added with your code. This way it will not override your coded column Names.

Sign up to request clarification or add additional context in comments.

4 Comments

i am not using database here lara.user already has one excel file which i need to import and then edit data in grid and export.now import and exporting is working fine just that grid is taking F1 as default column header which i need to replace with my column header as "Filename"
already tried that but its not working as the imported data from the available excel already has that column so its taking default as f1 only and when i perform GridView1.Columns[0].HeaderText = "Filename"; new column is getting added however the imported data from excel remains exactly same there's F1 in header then in first row filename then the contents of excel.i just need that imported excel header text to be filename in place of F1.
ok will try that but what about that F1 default value ?its not getting removed i even tried making columnheader property value as false but then my coded header values also become invisible so is there a way to add heading to datagrid cells? like in first row? my excel only has 1 column but after import another 3 columns need to be added.
snag.gy/Y0V8CZ.jpg please check this image Lara for better understanding.
0

resolved this myself just needed to change the Query from select * from [Sheet$] to select F1 as Filename from [Sheet$]

thanks for the help guys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.