私のコードを見つけてください。コンボボックスの選択と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;
}