在一个groupbox容器中有四个radioButton,怎么让我选择第一个radioButton时,dataGridView上显示数据库一个表的内容,选择第二个radioButton时,dataGridView上显示数据库另一个表的内容呢?
如数据库student中有四个表a,b,c,d,当我选择第一个radioButton时,dataGridView上显示a的内容,当选择第二个radioButton时,dataGridView上显示b的内容
要求具体代码
如数据库student中有四个表a,b,c,d,当我选择第一个radioButton时,dataGridView上显示a的内容,当选择第二个radioButton时,dataGridView上显示b的内容
要求具体代码
实例化SqlDataAdpater,并填充abcd四张表的数据到DataSet中
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("查询语句a表",SqlConnection对象);
da.Fill(ds,rbA.Name); 用RadioButton名称填充数据源
依此类推判断选择哪个radionbutton被选中
foreach循环Form中的radiobutton控件,判断哪个被选中,然后绑定到dataGridView
foreach(Control c in .this.Controls)
{
if(c is RadioButton)
{
RadioButton rb = c as RadioButton;
if(rb.Selected)
{
dataGridView1.DataSource = ds.Tables[rb.Name];
}
}
}
string sql = "SQL语句";
SqlConnection conn = new SqlConnection(str);
BindingSource b1 = new BindingSource();
DataGridView dataGridView1 = new DataGridView();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, conn);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
b1.DataSource = table;