我想根据组合框的选择获取或设置数据到DataGridView的每个单元
问题描述:
请找到我的代码,我需要知道组合框选择和datagridview之间的关系。基本上,如果我设置了我的区域选择,那么只有那些与区域相关的区域才会显示,而不是我现在正在获取的整个数据库。谢谢。我想根据组合框的选择获取或设置数据到DataGridView的每个单元
private void RTUModification_Load(object sender, EventArgs e) {
//Select cell where checkbox to be display
Rectangle rect = this.rtumodDGV1.GetCellDisplayRectangle(0, -1, true);
chkbox.Size = new Size(18, 18);
//set position of header checkbox where to places
rect.Offset(4, 2);
chkbox.Location = rect.Location;
chkbox.CheckedChanged += rtucheckBoxCheckChanged;
//Add CheckBox control to datagridView
this.rtumodDGV1.Controls.Add(chkbox);
// Create new DataTable instance
dtDataTable = new System.Data.DataTable();
if (null == dtDataTable)
{
MessageBox.Show("Insufficient memory.", "ERROR");
}
//load the datable and fill them with value
strErrorMsg = Utilities.OpenSQLConnection(out m_SqlConn, true, false); //load normal database
if (false == string.IsNullOrEmpty(strErrorMsg))
{
MessageBox.Show(strErrorMsg, "SQL connection ERROR");
}
SqlDataAdapter sqlAdapter = null;
strSelectCmd = "SELECT Area_ID, StationId, SystemId, CCNumber, LineNumber, RTUNumber, SRTUNumber, Description, SDescription FROM RTU_ADDRESS";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = m_SqlConn;
sqlCmd.CommandText = strSelectCmd;
sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = sqlCmd;
strErrorMsg = Utilities.FillDataTable(m_SqlConn, strSelectCmd, dtDataTable);
if (string.IsNullOrEmpty(strErrorMsg) == false)
{
MessageBox.Show("Failed to retrieve information from database.", "ERROR");
dtDataTable.Dispose();
Utilities.CloseAndDisposeSQLConnection(m_SqlConn);
}
int iRetrievedNoOfRecords = dtDataTable.Rows.Count;
if (iRetrievedNoOfRecords > 0)
{
for (int i = 0; i < iRetrievedNoOfRecords; i++)
this.rtumodcomboBox.Items.Add((string)dtDataTable.Rows[i]["Area_ID"]);
}
strErrorMsg = Utilities.ExecuteSQLCommand(m_SqlConn, strSelectCmd);
if (string.IsNullOrEmpty(strErrorMsg) == false)
{
MessageBox.Show(this, "Error!Loading into RTU datagrid");
}
//load the datagridview header
rtumodDGV1.DataSource = dtDataTable;
}
答
下面是您可能需要的示例代码。
strSelectCmd = "SELECT Area_ID, StationId, SystemId, CCNumber, LineNumber, RTUNumber, SRTUNumber, Description, SDescription FROM RTU_ADDRESS where Area_ID="+cbxMyCombox.SelectedIndex;
其他选项其他SelectedIndex可以是SelectedItem或SelectedValue。取决于你使用的是什么。目前尚不清楚。
+0
好,非常感谢 – user2177829 2013-03-16 20:15:07
+0
太棒了,你拯救了我的生命 – user2177829 2013-03-16 20:15:27
只是猜测,你没有在你的查询中添加任何where子句。你不觉得你需要在where子句中附加选定的区域。 – 2013-03-16 19:55:38
为什么我的帖子被拒绝?,谢谢Faheem – user2177829 2013-03-16 20:02:21
选择将能够显示所有字段,如果我把它放在哪里,它将如何链接到我的combobbox和datagridview ?.请为我写一段代码以了解更多。 – user2177829 2013-03-16 20:03:45