Datagridview按钮单击事件不触发 [英] Datagridview Button Click event not firing
问题描述
im使用以下代码填充Gridview.I在结尾添加2个按钮进行编辑和删除。查看点击事件..但在添加删除
按钮后,点击事件不会触发。我做错了什么?
private void BindGrid2()
{
try
{
string constr =Data Source = INSPIRE-1; +
初始目录= testdatabase; +
User id = testuser; +
Password = tester;;
using(SqlConnection con = new SqlConnection(constr))
{
string commandText =SELECT invnumber,itemname,quantity,rate FROM mytable2 where invnumber = @name;
using(SqlCommand command = new SqlCommand(commandText,con))
{
command.Parameters.AddWithValue(@ name,text_inv 。文本);
using(SqlDataAdapter sda = new SqlDataAdapter(command))
{
using(DataTable dt = new DataTable())
{
sda.Fill (dt);
dataGridView2.DataSource = dt;
}
}
}
if(flag2 == false)
{
flag2 = true;
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name =编辑;
uninstallButtonColumn.Text =编辑;
dataGridView2.Columns.Insert(0,uninstallButtonColumn);
dataGridView2.Columns [0] .DisplayIndex = 4;
DataGridViewButtonColumn uninstallButtonColumn2 = new DataGridViewButtonColumn();
uninstallButtonColumn.Name =Delete;
uninstallButtonColumn.Text =删除;
dataGridView2.Columns.Insert(5,uninstallButtonColumn2);
dataGridView2.Columns [5] .DisplayIndex = 5;
}
}
}
catch(异常错误)
{
MessageBox.Show(error.Message);
}
}
void dataGridView2_CellClick(object sender,DataGridViewCellEventArgs e)
{
var senderGrid =(DataGridView)sender;
string orderId;
if(e.ColumnIndex == 4)
{
try
{
orderId = (string)dataGridView1.SelectedCells [0] .OwningRow.Cells [1] .Value;
using(SqlConnection conn = new SqlConnection(constr))
{
try
{
conn.Open();
SqlDataReader myReader = null;
string commandText =select * from mytable2 where invnumber = @name;
SqlCommand命令= new SqlCommand(commandText,conn);
command.Parameters.AddWithValue(@ name,text_inv.Text);
myReader = command.ExecuteReader();
while(myReader.Read())
{
text_cname.Text = myReader [cname]。ToString();
text_quantity.Text = myReader [quantity]。ToString();
text_item.Text = myReader [itemname]。ToString();
text_rate.Text = myReader [rate]。ToString();
dateTimePicker1.Text = myReader [date]。ToString();
// textBox4.Text = myReader [stock]。ToString();
}
}
catch(异常错误)
{
MessageBox.Show(err.Message);
}
}
}
catch(异常错误)
{
}
}
else if(e.ColumnIndex == 5)
{
using(SqlConnection conn = new SqlConnection(constr))
{
orderId =(string)dataGridView1 .SelectedCells [0] .OwningRow.Cells [1] .Value;
conn.Open();
SqlDataReader myReader = null;
string commandText =delete from mytable2 where invnumber = @name;
SqlCommand命令= new SqlCommand(commandText,conn);
command.Parameters.AddWithValue(@ name,text_inv.Text);
myReader = command.ExecuteReader();
}
BindGrid2();
}
}
下面的更正
-
dataGridView2。 Columns.Insert(5,uninstallButtonColumn2)
todataGridView2.Columns.Insert(1,uninstallButtonColumn2)
$ b $如果(e.ColumnIndex == 4)到 -
orderId =(string)dataGridView1.SelectedCells [0] .OwningRow.Cells [1] .Value;
toorderId =(string)dataGridView1.SelectedCells [0] .OwningRow.Cells [2] .Value;
两个地方 -
else if(e.ColumnIndex == 5)
toelse if (e.ColumnIndex == 1)
if(e.ColumnIndex == 0)
im using the following code to populate a Gridview.I add 2 buttons for editing and deleting at the end.Hook to the click event.. but after i add the delete button the click events are not firing.What im i doing wrong?
private void BindGrid2()
{
try
{
string constr = "Data Source=INSPIRE-1;" +
"Initial Catalog=testdatabase;" +
"User id=testuser;" +
"Password=tester;";
using (SqlConnection con = new SqlConnection(constr))
{
string commandText = "SELECT invnumber,itemname,quantity,rate FROM mytable2 where invnumber= @name";
using (SqlCommand command = new SqlCommand(commandText, con))
{
command.Parameters.AddWithValue("@name", text_inv.Text);
using (SqlDataAdapter sda = new SqlDataAdapter(command))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView2.DataSource = dt;
}
}
}
if (flag2 == false)
{
flag2 = true;
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "Edit";
uninstallButtonColumn.Text = "Edit";
dataGridView2.Columns.Insert(0, uninstallButtonColumn);
dataGridView2.Columns[0].DisplayIndex = 4;
DataGridViewButtonColumn uninstallButtonColumn2 = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "Delete";
uninstallButtonColumn.Text = "Delete";
dataGridView2.Columns.Insert(5, uninstallButtonColumn2);
dataGridView2.Columns[5].DisplayIndex = 5;
}
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
string orderId;
if (e.ColumnIndex == 4)
{
try
{
orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value;
using (SqlConnection conn = new SqlConnection(constr))
{
try
{
conn.Open();
SqlDataReader myReader = null;
string commandText = "select * from mytable2 where invnumber= @name";
SqlCommand command = new SqlCommand(commandText, conn);
command.Parameters.AddWithValue("@name", text_inv.Text);
myReader = command.ExecuteReader();
while (myReader.Read())
{
text_cname.Text = myReader["cname"].ToString();
text_quantity.Text = myReader["quantity"].ToString();
text_item.Text = myReader["itemname"].ToString();
text_rate.Text = myReader["rate"].ToString();
dateTimePicker1.Text = myReader["date"].ToString();
// textBox4.Text = myReader["stock"].ToString();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
catch (Exception error)
{
}
}
else if (e.ColumnIndex == 5)
{
using (SqlConnection conn = new SqlConnection(constr))
{
orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value;
conn.Open();
SqlDataReader myReader = null;
string commandText = "delete from mytable2 where invnumber= @name";
SqlCommand command = new SqlCommand(commandText, conn);
command.Parameters.AddWithValue("@name", text_inv.Text);
myReader = command.ExecuteReader();
}
BindGrid2();
}
}
Corrections below
dataGridView2.Columns.Insert(5, uninstallButtonColumn2)
todataGridView2.Columns.Insert(1, uninstallButtonColumn2)
if (e.ColumnIndex == 4)
toif (e.ColumnIndex == 0)
orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value;
toorderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[2].Value;
at both the placeselse if (e.ColumnIndex == 5)
toelse if (e.ColumnIndex == 1)
这篇关于Datagridview按钮单击事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!