C# 从SQL表填充DataGridView数据,并从其他SQL Server表填充GridView组合框

C# 从SQL表填充DataGridView数据,并从其他SQL Server表填充GridView组合框,c#,C#,我在一个表单上有一个DataGridView从SQL Server表中获取记录,在DataGridView中有一个datagridviewcomboxcolumn从不同的SQL Server表中填充其数据。现在我想使用表2(组合框表)中的记录来填充表1。 这是一个Windows应用程序 //to fill the grid private void gridViewFillAddress() { //dataGridViewCashbook.DataSource = EmployeeDa

我在一个表单上有一个
DataGridView
从SQL Server表中获取记录,在
DataGridView
中有一个
datagridviewcomboxcolumn
从不同的SQL Server表中填充其数据。现在我想使用
表2(组合框表)
中的记录来填充表1。 这是一个Windows应用程序

//to fill the grid
private void gridViewFillAddress()
{
    //dataGridViewCashbook.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
    SqlConnection myConnection = new SqlConnection(@"Data Source = HP\SQL; Initial Catalog = Simpca; Integrated Security = True");
    String Query = "SELECT * FROM ADDRESSBOOKNEW";
    SqlCommand cmdDataBase = new SqlCommand(Query, myConnection);
    SqlDataAdapter sda = new SqlDataAdapter();
    sda.SelectCommand = cmdDataBase;
    DataTable dbdataset = new DataTable();
    sda.Fill(dbdataset);
    BindingSource bSource = new BindingSource();
    bSource.DataSource = dbdataset;
    dgvAdressBook.DataSource = dbdataset;
    sda.Update(dbdataset);
    dgvAdressBook.Columns["DateAddress"].DefaultCellStyle.Format = "yyyy/mm/dd";
}
//to fill the combo
void fillBANKERS()
{
    SqlConnection myConnection = new SqlConnection(@"Data Source = HP\SQL; Initial Catalog = Simpca; Integrated Security = True");
    String Query = "SELECT * FROM BANKLIST;";
    SqlCommand cmdDataBase = new SqlCommand(Query, myConnection);
    myConnection.Open();
    SqlDataAdapter sda = new SqlDataAdapter();
    sda.SelectCommand = cmdDataBase;
    DataTable dbdataset = new DataTable();
    sda.Fill(dbdataset);
    BANK.ValueMember = "BANKLISTNAME";
    BANK.DisplayMember = "BANKLISTNAME";
    DataRow topItem = dbdataset.NewRow();
    topItem[0] = "0";
    topItem[1] = "";
    dbdataset.Rows.InsertAt(topItem, 0);
    BANK.DataSource = dbdataset;
}
这是我的密码

private void dgvAdressBook_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    //to insert and modify           
     if (dgvAdressBook.CurrentRow != null)
     {
         SqlConnection myConnection = new SqlConnection(@"Data Sourc = HP\SQL; Initial Catalog = Simpca; Integrated Security = True");
         myConnection.Open();
         DataGridViewRow dgvRow = dgvAdressBook.CurrentRow;
         SqlCommand cmdDataBase = new SqlCommand("EditAddAddressBook", myConnection);
         cmdDataBase.CommandType = CommandType.StoredProcedure;
         if (dgvRow.Cells["id"].Value == DBNull.Value)
             cmdDataBase.Parameters.AddWithValue("@id", 0);
         else 
             cmdDataBase.Parameters.AddWithValue("@id", Convert.ToInt32(dgvRow.Cells["id"].Value));
         cmdDataBase.Parameters.AddWithValue("@name", dgvRow.Cells["NAME"].Value == DBNull.Value ? "": dgvRow.Cells["NAME"].Value.ToString());
         cmdDataBase.Parameters.AddWithValue("@ACCTNUMBERPAYEE", dgvRow.Cells["ACCTNUMBER"].Value == DBNull.Value ? "0" : dgvRow.Cells["ACCTNUMBER"].Value.ToString());                 
         //The Combo Box 
         cmdDataBase.Parameters.AddWithValue("@BANKERS", dgvRow.Cells["BANKERS"].Value == DBNull.Value ?  "" : dgvRow.Cells["BANKERS"].Value.ToString());
         cmdDataBase.Parameters.AddWithValue("@SORTCODE", Convert.ToInt32(dgvRow.Cells["SORT"].Value == DBNull.Value ? "0" : dgvRow.Cells["SORT"].Value));
         cmdDataBase.ExecuteNonQuery();
    }

我希望combobox列可以填充表1中银行家的现有记录,但在我选择下拉列表之前,它将返回空白。

使用Itemdatabound事件,如下所示

protected void dgvAdressBook_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
       foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
        {
          DataGridViewComboBoxCell BankersCombo = (DataGridViewComboBoxCell)(row.Cells[index of BankersCombo column]);

          BankersCombo.DataSource = // your contacts datasource;
          BankersCombo.DisplayMember = "name of field to be displayed like say ContactName";
          BankersCombo.ValueMember = "Id";
        }
}

我已经检查了DataGridView可用的事件列表,找不到ItemDataBound。我使用的是windows应用程序。在winforms中,您必须使用类似于此的内容,上述内容适用于webforms。