[英]Fill DataGridView data from SQL table and populate a GridView ComboBox from a different SQL Server table
I have a DataGridView
on a form that get it records from SQL Server table, I have a DataGridViewComboBoxColumn
in the DataGridView
that populates its data from a different SQL Server table.我在表单上有一个
DataGridView
,它从 SQL 服务器表中获取记录,我在DataGridView
中有一个DataGridViewComboBoxColumn
,它从不同的 SQL 服务器表中填充其数据。 Now I want to use the record from table2(ComboBox table)
to fill table 1. Its a Windows Application现在我想使用表 2
table2(ComboBox table)
中的记录来填充表 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;
}
This is my code这是我的代码
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();
}
I expect the combobox column to fill the existing records in table1 for bankers but it return blank until I select the drop down.我希望 combobox 列可以为银行家填充 table1 中的现有记录,但它返回空白,直到我 select 下拉。
use Itemdatabound event like below使用 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";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.