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



我在表单上有一个DataGridView,它从SQL Server表中获取记录,在DataGridView中有一个从其他SQL Server表填充数据的DataGridViewComboBoxColumn。现在我想使用table2(ComboBox table)中的记录来填充表1。这是一个Windows应用程序

//to fill the grid
private void gridViewFillAddress()
{
//dataGridViewCashbook.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
SqlConnection myConnection = new SqlConnection(@"Data Source = HPSQL; 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 = HPSQL; 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 = HPSQL; 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();
}

我希望组合框列为银行家填充表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";
}
}

最新更新