Click here to Skip to main content
12,244,651 members (51,853 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB ASP.NET Oracle VB.NET
my problem is i have a dropdown list in which i have 2 options gatepass and leave now for the leave gridview i have declared bound fields and i am populating it from a datasource let that datasource be dt1 now for gatepass (dt2) option i want to use the same gridview but the columns are not same in that datasource how can i use the same structure for both datasouces. is it even possible?
Posted 13-Jul-13 1:48am
Comments
Mahesh Bailwal 13-Jul-13 11:38am
   
Its not possible, but why you want to do that?
a2ulthakur 13-Jul-13 11:49am
   
because i have two totally unrelated datatables which i have to show conditionally in the same gridview... well and i guess its possible i just found out about dataset so i ll try and use that. if u know how to use that let me know.
Mahesh Bailwal 13-Jul-13 11:58am
   
Well if you want to bind one data-source at one time then its always possible but if you want to bind same grid with more than one data-source at the same time then its not possible and your dataset approach is correct.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

You can change the DataSource property of a DataGridView. For more information on how to effectively use the DataSource please refer to this link[^].

Please refer to below code, if should solve your problem:

DataTable _dt1;
DataTable _dt2;
 
public Form1()
{
    InitializeComponent();
 
    _dt1 = InitDataTableOne("Table 1");
    _dt2 = InitDataTableTwo("Table 2");
 
    dataGridView1.DataSource = _dt1;
 
    comboBox1.Items.Add(_dt1.TableName);
    comboBox1.Items.Add(_dt2.TableName);
 
    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
 
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    dataGridView1.DataSource = comboBox1.SelectedIndex == 0 ? _dt1 : _dt2;
}
 
private DataTable InitDataTableOne(string tableName)
{
    DataTable dt = new DataTable(tableName);
    dt.Columns.Add("Name");
    dt.Columns.Add("Age");
 
    dt.Rows.Add("A"+tableName, "20");
    dt.Rows.Add("B"+tableName, "24");
 
    return dt;
}
 
private DataTable InitDataTableTwo(string tableName)
{
    DataTable dt = new DataTable(tableName);
    dt.Columns.Add("Id");
    dt.Columns.Add("Address");
 
    dt.Rows.Add("A" + tableName, "Street 1");
    dt.Rows.Add("B" + tableName, "Street 2");
 
    return dt;
}


Thanks,
Ankush Bansal
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 14 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100