通过仅选择一个列值来填充DataGridView列

时间:2014-09-17 20:14:23

标签: c# sql winforms datagridview combobox

我有DataGridView,它由三列Profname, Profcharges and Profduedate填充。我已将Profname Column转换为一个comboBox,它由数据库中的所有Profnames填充。当任何Profname选择表格comboxBox时,我想自动填写Profcharges和Profduedate值。 我的代码是:

try{
    SqlConnection con = new SqlConnection(@"Data Source=AFZAL\SQLEXPRESS;Initial Catalog=GIMS_LabInfo;Integrated Security=True");
    con.Open();
    SqlCommand sc = new SqlCommand("SELECT PROFNAME, PROFCHARGES, PROFDUEDATE FROM PROFNAMES$", con);
    SqlDataReader reader;
    reader = sc.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Columns.Add("TEST NAME", typeof(string));
    dt.Load(reader);

    Column1.ValueMember = "Profname";
    Column1.DisplayMember = "Profname";
    Column1.DataSource = dt;
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

0 个答案:

没有答案