将DataGridView中的ComboBox绑定到DataTable [英] Bind ComboBoxes in DataGridView to DataTable

查看:82
本文介绍了将DataGridView中的ComboBox绑定到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的DataGridView中绑定2个组合框到DataTable中的2个字段(每个组合框一个字段),这样如果DataTable已经有一个值,该值将在组合框中显示
。如果组合框没有值,我希望组合框显示用户可以从中选择的值列表。我创建了一个绑定源,并将DataGridView(dgdMembers)数据源设置为该绑定源。我有
也将绑定源数据源设置为DataTable。当我填充DataTable时,行显示在我的DataGridView中。填充数据表的SELECT语句选择字段relActMSPStatus和relTransActionCd。这些是我想要绑定到2个组合框的字段。


需要一个如何执行此操作的示例,我的代码无法正常工作。谢谢。

 private void frmOutReach_Grid_Load(object sender,EventArgs e)
{
cboGroups.Items.AddRange(GetGroups( ));

bindingSource1 = new BindingSource();


//将DataGridView绑定到BindingSource
dgdMembers.DataSource = bindingSource1;


}

private void btnShow_Click(object sender,EventArgs e)
{
if(cboGroups.SelectedIndex == -1)
{
message =" Please select a insurer group" ;;
caption =" Insurer Group Select" ;;
MessageBoxButtons buttons = MessageBoxButtons.OK;

//显示MessageBox。
MessageBox.Show(this,message,caption,buttons);

返回;

}

GetCOBNonHorizo​​nRecsForGrp();

}

private void GetCOBNonHorizo​​nRecsForGrp()
{

CreateDataTable();

string GroupName = cboGroups.Text;

//填充dataTable
SqlCommand cmd1 = new SqlCommand(" Insurer_Select_Grid",sqlConnection1);
cmd1.Parameters.Clear();
cmd1.Parameters.Add(new SqlParameter(" @ InsurerGroup",GroupName));
cmd1.CommandType = CommandType.StoredProcedure;


da = new SqlDataAdapter(cmd1);
da.Fill(dataTable);
bindingSource1.DataSource = dataTable;




sqlConnection1.Close();

//调整DataGridView列的大小以适应新加载的内容。
dgdMembers.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);


setUpMemberGrid();
}

private void CreateDataTable()
{
//创建数据集
cobNonHorizo​​n = new DataSet(" cobNonHorizo​​n");

//创建dataTable
dataTable = new DataTable(" dataTable");

//为IKA跨度创建表
dtIKASpans = new DataTable(" dtIKASpans");
}


private void setUpMemberGrid()
{

//刷新数据网格视图
dgdMembers.Refresh() ;

//冻结列滚动
this.dgdMembers.Columns [" relHICN"]。Frozen = true;
this.dgdMembers.Columns [" relRelation"]。Frozen = true;
this.dgdMembers.Columns [" relSubscriberFirst"]。Frozen = true;
this.dgdMembers.Columns [" relSubscriberLast"]。Frozen = true;

//设置行号
setRowNumber();

//创建MSP ComboBox
dgdMembers.Columns [" relActMSPStatus"]。Visible = true;
DataGridViewComboBoxColumn cmbMSP = new DataGridViewComboBoxColumn();
cmbMSP.Items.Add("");
cmbMSP.Items.Add(" MSP");
cmbMSP.Items.Add(" Partial MSP");
cmbMSP.Items.Add(" No MSP");
cmbMSP.HeaderText =" Actual MSP" ;;
cmbMSP.Name =" MSPVal" ;;
cmbMSP.FlatStyle = FlatStyle.Flat;
cmbMSP.DataSource = dataTable;
cmbMSP.DisplayMember =" dataTable.relActMSPStatus" ;;
cmbMSP.ValueMember =" dataTable.relHICN" ;;
//dgdMembers.Columns.Add(cmbMSP);
dgdMembers.EditingControlShowing + = new DataGridViewEditingControlShowingEventHandler(dgdMembers_EditingControlShowing);

//创建交易代码ComboBox
dgdMembers.Columns [" relTransactionCd"]。Visible = true;
DataGridViewComboBoxColumn cmbTransCd = new DataGridViewComboBoxColumn();
LoadTrans();
cmbTransCd.DataSource = Srcds.Tables [0];
cmbTransCd.DisplayMember =" SrcVal" ;;
cmbTransCd.ValueMember =" SrcVal" ;;
cmbTransCd.HeaderText =" Trans Action" ;;
cmbTransCd.Name =" TransAction" ;;
cmbTransCd.FlatStyle = FlatStyle.Flat;
cmbTransCd.DropDownWidth = 200;
cmbTransCd.Width = 200;
dgdMembers.Columns.Add(cmbTransCd);


//不显示组名
dgdMembers.Columns [" Group Name"]。Visible = true;

//灰色显示已确定Trans Action代码和MSP值的行
GrayOutRow();

//加载IKA的调用过程
RefreshDataGrid();

/ * DetermineStatus(); * /

}

解决方案

将重新发布

I would like to bind 2 combo boxes in my DataGridView to 2 fields (one field per combo box) in a DataTable so that if the DataTable already has a value, that value will show in the combobox. If the combo boxes don't have a value, I would like the combo box to show a list of values the user can select from. I have created a binding source and have set the DataGridView (dgdMembers) datasource to that binding source. I have also set the binding source datasource to a DataTable. When I fill the DataTable the rows appear in my DataGridView. The SELECT statement to fill the datatable is selecting the fields relActMSPStatus and relTransActionCd. These are the fields that I would like to bind to the 2 comboboxes.

Need a example of how to do this, my code is not working. Thank you.

private void frmOutReach_Grid_Load(object sender, EventArgs e)
        {
            cboGroups.Items.AddRange(GetGroups());

            bindingSource1 = new BindingSource();


            // Bind the DataGridView to the BindingSource
            dgdMembers.DataSource = bindingSource1;


        }

private void btnShow_Click(object sender, EventArgs e)
        {
            if (cboGroups.SelectedIndex == -1)
            {
                message = "Please select an insurer group";
                caption = "Insurer Group Select";
                MessageBoxButtons buttons = MessageBoxButtons.OK;

                // Display the MessageBox.
                MessageBox.Show(this, message, caption, buttons);

                return;
               
            }

            GetCOBNonHorizonRecsForGrp();

        }

 private void GetCOBNonHorizonRecsForGrp()
        {

            CreateDataTable();

            string GroupName = cboGroups.Text;

            //Populate dataTable
            SqlCommand cmd1 = new SqlCommand("Insurer_Select_Grid", sqlConnection1);
            cmd1.Parameters.Clear();
            cmd1.Parameters.Add(new SqlParameter("@InsurerGroup", GroupName));
            cmd1.CommandType = CommandType.StoredProcedure;


            da = new SqlDataAdapter(cmd1);
            da.Fill(dataTable);
            bindingSource1.DataSource = dataTable;
           

           

            sqlConnection1.Close();

            // Resize the DataGridView columns to fit the newly loaded content.
            dgdMembers.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCells);


            setUpMemberGrid();
        }

 private void CreateDataTable()
        {
            //Create dataset
            cobNonHorizon = new DataSet("cobNonHorizon");

            //Create dataTable
            dataTable = new DataTable("dataTable");

            //Create table for IKA spans
            dtIKASpans = new DataTable("dtIKASpans");
        }


private void setUpMemberGrid()
        {

            //Refresh Data Grid View
            dgdMembers.Refresh();

            //Freeze columns for scrolling
            this.dgdMembers.Columns["relHICN"].Frozen = true;
            this.dgdMembers.Columns["relRelation"].Frozen = true;
            this.dgdMembers.Columns["relSubscriberFirst"].Frozen = true;
            this.dgdMembers.Columns["relSubscriberLast"].Frozen = true;

            //Set row number
            setRowNumber();

            //Create MSP ComboBox
            dgdMembers.Columns["relActMSPStatus"].Visible = true;
            DataGridViewComboBoxColumn cmbMSP = new DataGridViewComboBoxColumn();
            cmbMSP.Items.Add(" ");
            cmbMSP.Items.Add("MSP");
            cmbMSP.Items.Add("Partial MSP");
            cmbMSP.Items.Add("No MSP");
            cmbMSP.HeaderText = "Actual MSP";
            cmbMSP.Name = "MSPVal";
            cmbMSP.FlatStyle = FlatStyle.Flat;
            cmbMSP.DataSource = dataTable;
            cmbMSP.DisplayMember = "dataTable.relActMSPStatus";
            cmbMSP.ValueMember = "dataTable.relHICN";
            //dgdMembers.Columns.Add(cmbMSP);
            dgdMembers.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgdMembers_EditingControlShowing);

            //Create Transaction Code ComboBox
            dgdMembers.Columns["relTransactionCd"].Visible = true;
            DataGridViewComboBoxColumn cmbTransCd = new DataGridViewComboBoxColumn();
            LoadTrans();
            cmbTransCd.DataSource = Srcds.Tables[0];
            cmbTransCd.DisplayMember = "SrcVal";
            cmbTransCd.ValueMember = "SrcVal";
            cmbTransCd.HeaderText = "Trans Action";
            cmbTransCd.Name = "TransAction";
            cmbTransCd.FlatStyle = FlatStyle.Flat;
            cmbTransCd.DropDownWidth = 200;
            cmbTransCd.Width = 200;
            dgdMembers.Columns.Add(cmbTransCd);
            

            //Don't display group name
            dgdMembers.Columns["Group Name"].Visible = true;

            //Gray out rows where the Trans Action code and MSP values have been determined
            GrayOutRow();

            //Call procedure to load IKA spans
            RefreshDataGrid();

            /* DetermineStatus(); */

        }

解决方案

Will repost


这篇关于将DataGridView中的ComboBox绑定到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆