我的数据网格视图中有一个数据网格视图组合框列。在表单的加载事件中,我在组合框列中加载公司名称,并通过单元格格式化事件,我将默认值提供给数据网格视图组合框列。
现在,如果我从组合框列中选择不同的值,则不会反映出来。意味着只有默认值。
我的加载和单元格格式代码是
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();
}
并且加载事件是
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"]));
}
}
实际上我使用相同的数据网格视图进行插入和更新操作。我希望当用户从组合框列表中选择不同的值时,默认值应该被选中的值替换。
提前感谢...
答案 0 :(得分:0)
CellFormatting
事件处理程序不是设置默认值的正确位置。只要需要格式化单元格的内容以进行显示,就会发生CellFormatting。无论何时更改组合框中的值并接受该值,都会引发此事件,并且默认值代码会重置用户选择。
在DataGridView.RowsAdded
中,您可以为新添加的行设置组合框单元格的defalut值
示例代码:
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;
}
}
答案 1 :(得分:0)
您还可以绑定 DataBindingComplete 事件。前一个和这个答案的唯一区别是@Junaith绑定 RowAdded 事件意味着它会在每次添加行时调用该函数。在我的案例中,我绑定了 DataBindingComplete 事件,该事件将在网格满载后触发,这意味着这将只调用一次,那时它将检查所有行的网格第一列中的所有复选框
mygrid.DataBindingComplete +=
new DataGridViewBindingCompleteEventHandler(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;
}
}
答案 2 :(得分:0)
你可以在RowAdded Event或DataGridview comboboxcolumn创建时使用它。它将设置默认值
comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
答案 3 :(得分:-1)
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = e.RowIndex; i < (e.RowIndex+e.RowCount); i++)
{
comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
}
}