InPowerS.Net

 找回密碼
 註冊
搜索
查看: 1975|回復: 0

DataGridView內包含ComboBox列 實現選定ComboBox的項資料的聯動.

[複製鏈接]
發表於 2010-9-1 19:16:08 | 顯示全部樓層 |閱讀模式
首先要綁定 ComboBox 然後綁定DataGridView

  1.              DataSet dsProp = new DataSet();
  2.              DataSet dsCond = new DataSet();
  3.              DataSet dsCond1 = new DataSet();
  4.              DataSet dsCond2 = new DataSet();
  5.             try
  6.             {
  7.                 dsProp = App.DbService.GetStandardControlProp(intPrjCode, intStationId, intModuleId, intPointId);
  8.                 dsCond = App.DbService.GetStandardControlCond(intPrjCode,intStationId, intModuleId,1);
  9.                 dsCond1 = App.DbService.GetStandardControlCond(intPrjCode, intStationId, intModuleId, 2);
  10.                 dsCond2 = App.DbService.GetStandardControlCond(intPrjCode, intStationId, intModuleId, 3);
  11.                 dsStationInfo = App.DbService.getStationNameDs(intPrjCode);
  12.                
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 MessageBox.Show(ex.Message, "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Error);
  17.                 return;
  18.             }
  19.             if (dsProp == null || dsCond==null)
  20.             {
  21.                 MessageBox.Show("數據載入失敗!");
  22.                 return;
  23.             }
  24.                         
  25.             DataRow drNew = dsStationInfo.Tables[0].NewRow();
  26.             drNew["stationid"] = System.DBNull.Value;
  27.             drNew["stationnm"] = System.DBNull.Value;
  28.             dsStationInfo.Tables[0].Rows.InsertAt(drNew, 0);
  29.             StationID.DataSource = dsStationInfo.Tables[0];
  30.             StationID.DisplayMember = "STATIONNM";
  31.             StationID.ValueMember = "STATIONID";
  32.             MODULEID.DataSource = dsCond.Tables[0];
  33.             MODULEID.DisplayMember = "MODULEID";
  34.             MODULEID.ValueMember = "MODULEID";
  35.             POINTNM.DataSource = dsCond.Tables[0];
  36.             POINTNM.DisplayMember = "POINTNAME";
  37.             POINTNM.ValueMember = "POINTID";
  38.             StationID2.DataSource = dsStationInfo.Tables[0];
  39.             StationID2.DisplayMember = "STATIONNM";
  40.             StationID2.ValueMember = "STATIONID";
  41.             MODULENM2.DataSource = dsCond1.Tables[0];
  42.             MODULENM2.DisplayMember = "MODULEID";
  43.             MODULENM2.ValueMember = "MODULEID";
  44.             POINTNM2.DataSource = dsCond1.Tables[0];
  45.             POINTNM2.DisplayMember = "POINTNAME";
  46.             POINTNM2.ValueMember = "POINTID";
  47.             StationID3.DataSource = dsStationInfo.Tables[0];
  48.             StationID3.DisplayMember = "STATIONNM";
  49.             StationID3.ValueMember = "STATIONID";
  50.             MODULENM3.DataSource = dsCond2.Tables[0];
  51.             MODULENM3.DisplayMember = "MODULEID";
  52.             MODULENM3.ValueMember = "MODULEID";
  53.             POINTNM3.DataSource = dsCond2.Tables[0];
  54.             POINTNM3.DisplayMember = "POINTNAME";
  55.             POINTNM3.ValueMember = "POINTID";
  56.             
  57.             dgvList.DataSource = InitDT(dsCond.Tables[0].Copy(), 10, 0);
  58.             dgvList2.DataSource = InitDT(dsCond1.Tables[0].Copy(), 7, 0);
  59.             dgvList3.DataSource = InitDT(dsCond2.Tables[0].Copy(), 7, 1);
複製代碼
注意設定ComboBox的屬性:DataPropertyName ,然後調用CellValueChanged事件,此事件主要完成在選定ComboBox時重新綁定要改變的ComboBox.

  1. private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  2.         {
  3.             dgvCellAutoSetValue(e, dgvList);
  4.         }private void dgvCellAutoSetValue(DataGridViewCellEventArgs e, DataGridView dgv)
  5.         {
  6.             if (e.ColumnIndex == 1)
  7.             {
  8.                 if (e.RowIndex > -1)
  9.                 {
  10.                     if (dgv.Rows[e.RowIndex].Cells[1].Value == System.DBNull.Value)
  11.                     {
  12.                         return;
  13.                     }
  14.                     DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dgv.Rows[e.RowIndex].Cells[2];
  15.                     try
  16.                     {
  17.                         DataTable DtModule = App.DbService.GetModuleIDInfo(intPrjCode, Convert.ToInt16(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)).Tables[0];
  18.                         if (DtModule.Rows.Count > 0)
  19.                         {
  20.                             cell.DataSource = DtModule;
  21.                            
  22.                             cell.DisplayMember = "MODULEID";
  23.                             cell.ValueMember = "MODULEID";
  24.                             if (cell.Items.Count > 0)
  25.                             {
  26.                                 cell.Value = ((DataRowView)cell.Items[0]).Row[cell.ValueMember];
  27.                             }
  28.                         }
  29.                     }
  30.                     catch (Exception ex)
  31.                     {
  32.                         MessageBox.Show(ex.Message);
  33.                     }
  34.                 }
  35.             }
  36.             else if (e.ColumnIndex == 2)
  37.             {
  38.                 if (e.RowIndex > -1)
  39.                 {
  40.                     if (dgv.Rows[e.RowIndex].Cells[2].Value == System.DBNull.Value || dgv.Rows[e.RowIndex].Cells[1].Value==DBNull.Value)
  41.                     {
  42.                         return;
  43.                     }
  44.                     int stationid = Convert.ToInt16(dgv.Rows[e.RowIndex].Cells[1].Value);
  45.                     int moduleid = Convert.ToInt16(dgv.Rows[e.RowIndex].Cells[2].Value);
  46.                     DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dgv.Rows[e.RowIndex].Cells[3];
  47.                     try
  48.                     {
  49.                         DataTable DtPoint = App.DbService.GetPointInfo(intPrjCode, stationid, moduleid).Tables[0];
  50.                         if (DtPoint.Rows.Count > 0)
  51.                         {
  52.                             cell.DataSource = DtPoint;
  53.                             cell.DisplayMember = "POINTNAME";
  54.                             cell.ValueMember = "POINTID";
  55.                             if (cell.Items.Count > 0)
  56.                             {
  57.                                 cell.Value = ((DataRowView)cell.Items[0]).Row[cell.ValueMember];
  58.                             }
  59.                         }
  60.                     }
  61.                     catch (Exception ex)
  62.                     {
  63.                         MessageBox.Show(ex.Message);
  64.                     }
  65.                 }
  66.             }
  67.         }

複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2025-4-11 20:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表