0

I am working with different datasources which is having different columns. For example DataTable1 has A,B and C Column. and DataSource2 has X,Y,Z Columns.

In my windows form application I am allowing user to browse a certain Excel file with Format1 (A,B,C) and Format2 (X,Y,Z). when I load first datasource1 in grid view it shows columns A,B,C and it's data from excel. But when I load Format2. Datagridview retains the format from Datasource1 and shows (A,B,C,X,Y,Z) columns.

Please note that I have common datagridview for both data source. Also, I have tried following

DataTable.Clear();
DataTable.Columns.Remove("");
AutoGeneratedColumns=False;

How do I make sure that datagridview shows only the current Datasource related columns while user is selecting the excel.

DataSource1 - Format 1 - A,B,C DataSource2 - Format 2 - X,Y,Z

1
  • why are you set AutoGeneratedColumns=False ? Commented Jul 24, 2019 at 4:51

2 Answers 2

2

this is working for me:

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("A");
dt1.Columns.Add("B");
dt1.Columns.Add("C");

dt2.Columns.Add("X");
dt2.Columns.Add("Y");
dt2.Columns.Add("Z");

dt1.Rows.Add("a", "a", "a");
dt2.Rows.Add("b", "b", "b");

private void Button1_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = dt1;  // set datasource from dt1
}

private void Button2_Click(object sender, EventArgs e)
{
       dataGridView1.DataSource = dt2; // set datasource from dt2
}
2
  • I agree; in a situation where a single datagridview is required to show different sets of data the data should be separated into different datatables and they be swapped in and out. I would also consider having a tab page control and two different grids to allow for easy development and give the user a way to flick back and forth and more easily see which was which but that's effectively she same solution as you have here just with different controls/slightly different UI
    – Caius Jard
    Commented Jul 24, 2019 at 5:00
  • you can use from new DataTable() in each action. like another my answer. Commented Jul 24, 2019 at 5:20
0

Try this:

Datagridview.Columns.Clear();
Datagridview.Rows.Clear();
Datagridview.Refresh();
Datagridview.DataSource = null; 
3
  • This is unlikely to help. The problem is that extra columns are showing that are not wanted. So clearing the rows will do nothing. Additionally this is a data bound girdview so changes to what it should should be made to the datatable it is showing, not to the grid itself
    – Caius Jard
    Commented Jul 24, 2019 at 4:57
  • I don't think so, the problem is: Datagridview retains the format from Datasource1 and shows (A,B,C,X,Y,Z) columns.
    – Roxana Sh
    Commented Jul 24, 2019 at 5:03
  • None of these statements are in any way useful to the person asking the question. When you change the DataSource property of a DataGridView it will erase all its columns and rows and build new ones from the new DataSource. Try ARF's code if you don't believe; he doesn't clear anything on the DGV he just swaps the DataSource
    – Caius Jard
    Commented Jul 24, 2019 at 5:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.