简体   繁体   English

如何使用本身的选定值更改数据网格视图组合框列的默认值

[英]How to change the default value of a data grid view combo box column with selected value from itself

i have a data grid view combo box column in my data grid view. 我在数据网格视图中有一个数据网格视图组合框列。 On on load event of form i am loading the companies name in combo box column and through cell formatting event i am giving the default value to data grid view combo box column. 在表单加载事件中,我正在组合框列中加载公司名称,而在单元格格式化事件中,我正在为数据网格视图组合框列提供默认值。

Now if i select different value from combo box column it does not get reflected. 现在,如果我从组合框列中选择其他值,则不会得到反映。 means only default value is there. 表示仅存在默认值。

My code for on load and cell formatting is 我的加载和单元格格式化代码是

   public void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        SqlConnection c = new SqlConnection();
        c.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='D:\\Documents\\Visual Studio 2008\\Projects\\Accounts\\Accounts\\Database1.mdf';Integrated Security=True;User Instance=True";
        c.Open();

        if (e.ColumnIndex == 1 && Viewcashvoucher.mk != "")
        {
            string mky = Viewcashvoucher.mk;
                 string q = "select * from lgr where main_key ='" + mky + "'";
                 SqlCommand cmd = new SqlCommand(q, c);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            e.Value = dr["account_n"].ToString();

        }
        }
        c.Close();
    }

and on load event is 并且在加载事件是

private void Cash_Voucher_Load(object sender, EventArgs e)
    {
        string mky = Viewcashvoucher.mk;
        SqlConnection c = new SqlConnection();
        c.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='D:\\Documents\\Visual Studio 2008\\Projects\\Accounts\\Accounts\\Database1.mdf';Integrated Security=True;User Instance=True";
        c.Open();


            string q = "select max(date) from lgr ";
            SqlCommand cmd = new SqlCommand(q, c);
            SqlDataReader rd = cmd.ExecuteReader();
            try
            {
                if (rd.Read())
                {
                    DateTime date = rd.GetDateTime(0);
                    if (date != null)
                    {
                        tbdate.Text = date.ToShortDateString();
                    }
                }
            }
            catch (Exception ex)
            {
                tbdate.Text = DateTime.Now.ToShortDateString();
            }
            c.Close();
            c.Open();
            string q1 = "select account_n from master";
            SqlCommand cmd1 = new SqlCommand(q1, c);
            SqlDataReader rd1 = cmd1.ExecuteReader();
            DataGridViewComboBoxColumn acname = dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
            while (rd1.Read())
            {
                acname.Items.Add(Convert.ToString(rd1["account_n"]));


            }

} }

actually i am using same data grid view for insert and update operation. 实际上,我使用相同的数据网格视图进行插入和更新操作。 i want that as user select different value from combo box list the default value should be replaced by selected one. 我希望当用户从组合框列表中选择其他值时,默认值应替换为所选值。

thanks in advance... 提前致谢...

CellFormatting event handler is not the right place to set the default value. CellFormatting事件处理程序不是设置默认值的正确位置。 CellFormatting occurs whenever the content of the cells needs to be formated for display. 每当单元格的内容需要格式化才能显示时,就会发生CellFormatting。 Whenever you the change the value in the combobox and accept the value, this event is raised and the default value code resets the user selection. 每当您更改组合框中的值并接受该值时,都会引发此事件,并且默认值代码将重置用户选择。

In DataGridView.RowsAdded you could set the defalut value for the combobox cell for the newly added row(s) DataGridView.RowsAdded Leicester中,您可以为新添加的行设置组合框单元格的默认值

Sample Code: 样例代码:

void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        for (int i = e.RowIndex; i < (e.RowIndex+e.RowCount); i++)
        {
            dataGridView1.Rows[i].Cells[comboboxcolumn_index].Value = defaultvalue;
        }            
    }

you can also bind the DataBindingComplete event. 您还可以绑定DataBindingComplete事件。 The only difference in previous and this answer is @Junaith bind the RowAdded event mean it will call the function every time you add the row. 以前和此答案的唯一区别是@Junaith绑定RowAdded事件,这意味着它将在每次添加行时调用该函数。 and here in mycase I bound DataBindingComplete event which will fire after the grid is full loaded mean this will call only once and that time it will check the all the checkbox in first column of the grid for all the lines. 在mycase中,我绑定了DataBindingComplete事件,该事件将在网格完全加载后触发,这意味着将仅调用一次,然后它将检查网格第一列中所有行的所有复选框。

mygrid.DataBindingComplete +=
       new DataGridViewBindingCompleteEventHandler(mygrid_DataBindingComplete);

then and you have to define a function name as mygrid_DataBindingComplete 然后,您必须将函数名称定义为mygrid_DataBindingComplete

    private void mygrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        DataGridView temp = (DataGridView)sender;

        foreach (DataGridViewRow rw in temp.Rows)
        {
            // here 0 indicating the checbox column is the first column in grid
            // first column so index would be 0.  true will check the checkbox
            rw.Cells[0].Value = true;
        }
     }

hai you can Just use this on RowAdded Event or DataGridview comboboxcolumn creation time. 您可以在RowAdded Event或DataGridview comboboxcolumn创建时间上使用它。 It will Set the Default Value 它将设置默认值

comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    for (int i = e.RowIndex; i < (e.RowIndex+e.RowCount); i++)
    {
        comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM