c# datagirdview control embedded combox method and embedded combox can drop down and edit and combox load database data

First drag and drop a datagirdview from the toolbar, and then operate according to the picture selection. Write picture description here
After the selection is completed, a datagirdview with a drop-down box is added successfully, and then the data is loaded and bound in the background. Here I wrote a service to get the drop-down data from the server database
Write picture description here
At this time, there are a total of three boxes. The first box takes data from the service and returns a datatable.
Then this.Analy.ValueMember sets the value field, and the display field is set below.
The next part is to take the data only two fields from the datatable, you can also not do this, I just want to understand.
Then converted into dataview type assigned to combox I found combox in a row so this type is dataviewrow get it, then take the line from the table, assigned to combox, then drop down to do now can edit
Write picture description hereadd this one key word: this .dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.EnabledComboBoxWrite);
Then there is the code in the two methods, you can add and modify it
yourself private void EnabledComboBoxWrite(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e. Control as ComboBox;
if (cb != null)
{
cb.DropDownStyle = ComboBoxStyle.DropDown;
cb.Validating += new System.ComponentModel.CancelEventHandler(cb_Validating);
}

    }
    void cb_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
        if (cbo.Text.Trim() == string.Empty) return;

        DataGridView grid = cbo.EditingControlDataGridView;
        object value = cbo.Text;

        if (cbo.Items.IndexOf(value) == -1)
        {
            DataRow[] drs = Dtb_KYData3.Select("V_1_10000005='" + value+"'");
            if (drs.Length > 0) return;
            DataGridViewComboBoxColumn cboCol = (DataGridViewComboBoxColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
            DataTable dtv=Dtb_KYData3.Clone();
            DataRow drv = dtv.NewRow();
            DataRow drv3 = Dtb_KYData3.NewRow();
            drv["V_1_10000005"] = value;
            drv3["V_1_10000005"] = value;
            drv["ID"] = value;
            Dtb_KYData3.Rows.Add(drv3);
            dtv.Rows.Add(drv);
            DataView dv = new DataView(dtv);
            foreach (DataRowView drvo in dv)
            {
                cbo.Items.Add(drvo);
                cboCol.Items.Add(drvo);

     grid.CurrentCell.Value = drvo;

}
}
}
} To this function is completed Write picture description hereafter typing the drop-down will automatically add the data you input Larry down the
program source code Download: https://download.csdn.net/download/hello_mr_anan/10633390

Guess you like

Origin blog.csdn.net/hello_mr_anan/article/details/81448103