我想根据主要活动ID列填充Windows窗体中DataGridView
的下拉列。
我有main activity id
以及从存储过程返回的DataGridView
中的其他详细信息。因此,当用户单击子活动列时,我需要根据此父ID填充子活动,然后根据此子活动,我需要填充其他下拉列。
╔══════════════╦══════════════════╦══════════╗
║ ActivityId ║ ActivityName ║ ParentId ║
╠══════════════╬══════════════════╬══════════╣
║ 10611801 ║ FootPath ║ 106118 ║
║ 10611802 ║ Staggared Drains ║ 106118 ║
║ 10611901 ║ Manual Weeding ║ 106119 ║
║ 10611902 ║ Chemical Weeding ║ 106119 ║
╚══════════════╩══════════════════╩══════════╝
我试图在DataGridView.CellClick
事件中填充列,但我得到了
无效值异常
关于不同的主动性。
public partial class CustomDropDown : Form
{
public CustomDropDown()
{
InitializeComponent();
}
public void BindGridData()
{
string selectQuery = "Select ActivityId as MainActivityId,ActivityName as MainActivity from ACTIVITY_MASTER_MST where ParentId is null";
var mainDataSource = Common.GetDatas(selectQuery);
dataGridView1.DataSource = mainDataSource;
string sourceQuery = "Select convert(varchar,ActivityId) as SubActivityId,ActivityName as SubActivityName from ACTIVITY_MASTER_MST";
var subActivityDataSource =Common.GetDatas(sourceQuery);
PopulateGridDropDown(SubActivityId, subActivityDataSource, "SubActivityName", "SubActivityId");
}
public void PopulateGridDropDown(DataGridViewComboBoxColumn cbo, DataTable source, string displayMember, string valueMember)
{
cbo.DataSource = source;
cbo.DisplayMember = displayMember;
cbo.ValueMember = valueMember;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var index = dataGridView1.CurrentCell.ColumnIndex;
var rowIndex = dataGridView1.CurrentCell.RowIndex;
string columnName = this.dataGridView1.Columns[index].Name;
switch (columnName)
{
case "SubActivityId":
var parentId = dataGridView1[MainActivityId.Name, rowIndex].Value;
var strQuery = "Select convert(varchar,ActivityId) as SubActivityId,ActivityName as SubActivityName from ACTIVITY_MASTER_MST where ParentId=" + Convert.ToInt32( parentId);
DataTable source = new DataTable();
source = Common.GetDatas(strQuery);
PopulateGridDropDown(SubActivityId, source, "SubActivityName", "SubActivityId");
break;
default:
break;
}
}
private void CustomDropDown_Load(object sender, EventArgs e)
{
BindGridData();
}
}
请分享您的想法?