如何根据另一个组合框的选择填充datagridview [英] how to fill datagridview based on selection of another combobox
问题描述
我有ComboBox Contain faculties,然后在选择任何教员后我需要填写DataGridView By Details的详细信息,详细信息是部门和每个部门的数量,所有部门必须加载到组合框内部datagridvew,我的意思是当我选择教师
datagridvew中的组合框必须包含该教师的部门和同一行中该教师的学生人数,当我从combbox中选择另一个部门时,学生人数必须更改
谢谢你^^
>>当我选择时来自combbox的另一个部门,学生人数必须更改
请参考以下演示,我的数据库中有一个表:
代码:
private DataTable CreateTable()
{
DataTable T_Gender = new DataTable();
T_Gender.Columns.AddRange(new DataColumn [] {
new DataColumn(" DepartmentID"),
new DataColumn(" DepartmentName"),
});
T_Gender.Rows.Add(new object [] {" 1"," Math"});
T_Gender.Rows.Add(new object [] {" 2"," History"});
T_Gender.Rows.Add(new object [] {" 3"," English"});
返回T_Gender;
}
private void FrmDgvComboBox_Load(object sender,EventArgs e)
{
DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.DataSource = CreateTable();
cb.HeaderText =" Department" ;;
cb.DisplayMember =" DepartmentName" ;;
cb.ValueMember =" DepartmentID" ;;
dataGridView1.Columns.Insert(0,cb);
}
private void dataGridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridView1.CurrentCell.RowIndex!= -1&& dataGridView1.CurrentCell .ColumnIndex == 0)
{
((ComboBox)e .Control).SelectedIndexChanged + = new EventHandler(Form1_SelectedIndexChanged);
}
}
private void Form1_SelectedIndexChanged(object sender,EventArgs e)
{
DataGridViewComboBoxCell dgvcmbcell = dataGridView1 [0,0] as DataGridViewComboBoxCell;
String text = dgvcmbcell.EditedFormattedValue.ToString();
int count;
string strCon = @" Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\ Users \v-baf \ OneDrive \ MyWork \ MyWinFrm \ WinFormApplication20170302 \WinFormApplication20170405 + \ Data \DatabaseTest.mdf; Integrated Security = True" ;;
string sqlSelect = @"从Table1中选择count,其中Department ='" + text +"'" ;;
using(SqlConnection conn = new SqlConnection(strCon))
{
using(SqlCommand cmd = new SqlCommand(sqlSelect,conn))
{
conn.Open( );
count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
}
}
dataGridView1.Rows [0] .Cells [1] .Value = count;
}
希望有所帮助!
最好的问候,
Stanly
I have ComboBox Contain faculties ,then after select any faculty I need to fill DataGridView By Details of this faculty , the details are departments and number of every ,all department must loaded into combobox inner datagridvew ,I mean when I select faculty the combobox in datagridvew must contain departments of this faculty and number of students in this faculty in the same row ,and when I select another department from combbox the number of student must be change
thank you ^^
Hi,
>>when I select another department from combbox the number of student must be change
Please refer to the following demo, I have a table in my database:
And the code:
private DataTable CreateTable() { DataTable T_Gender = new DataTable(); T_Gender.Columns.AddRange(new DataColumn[] { new DataColumn("DepartmentID"), new DataColumn("DepartmentName"), }); T_Gender.Rows.Add(new object[] { "1", "Math" }); T_Gender.Rows.Add(new object[] { "2", "History" }); T_Gender.Rows.Add(new object[] { "3", "English" }); return T_Gender; } private void FrmDgvComboBox_Load(object sender, EventArgs e) { DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn(); cb.DataSource = CreateTable(); cb.HeaderText = "Department"; cb.DisplayMember = "DepartmentName"; cb.ValueMember = "DepartmentID"; dataGridView1.Columns.Insert(0, cb); } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCell.RowIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 0) { ((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(Form1_SelectedIndexChanged); } } private void Form1_SelectedIndexChanged(object sender, EventArgs e) { DataGridViewComboBoxCell dgvcmbcell = dataGridView1[0, 0] as DataGridViewComboBoxCell; String text = dgvcmbcell.EditedFormattedValue.ToString(); int count; string strCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\v-baf\OneDrive\MyWork\MyWinFrm\WinFormApplication20170302\WinFormApplication20170405+\Data\DatabaseTest.mdf;Integrated Security=True"; string sqlSelect = @"select count from Table1 where Department='" + text + "'"; using (SqlConnection conn = new SqlConnection(strCon)) { using (SqlCommand cmd = new SqlCommand(sqlSelect, conn)) { conn.Open(); count = Convert.ToInt32(cmd.ExecuteScalar()); conn.Close(); } } dataGridView1.Rows[0].Cells[1].Value = count; }Hope it helps!
Best Regards,
Stanly
这篇关于如何根据另一个组合框的选择填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!