我使用以下代码将数据绑定到DataGridview
private void BindGrid()
{
try
{
string constr = "Data Source=INSPIRE-1;" +
"Initial Catalog=testdatabase;" +
"User id=testuser;" +
"Password=tester;";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM mytable", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
if (flag == false)
{
flag = true;
DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "Edit";
uninstallButtonColumn.Text = "Edit";
dataGridView1.Columns.Insert(4, uninstallButtonColumn);
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
我通过挂钩到datagridview行中的按钮点击事件来更新所选记录,如下所示
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex ==4)
{
button4.Enabled = false;
try
{
orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value;
using (SqlConnection conn = new SqlConnection(constr))
{
try
{
conn.Open();
SqlDataReader myReader = null;
string commandText = "select * from mytable where name= @name";
SqlCommand command = new SqlCommand(commandText, conn);
command.Parameters.AddWithValue("@name", orderId);
myReader = command.ExecuteReader();
while (myReader.Read())
{
textBox1.Text = myReader["name"].ToString();
textBox2.Text = myReader["age"].ToString();
textBox3.Text = myReader["phone"].ToString();
textBox4.Text = myReader["address"].ToString();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
catch (Exception error)
{
}
}
}
现在我使用下面的代码更新值
private void button5_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("UPDATE mytable SET name=@NewName,age=@NewAge,phone=@NewPhone,Address=@NewAddress" +
" WHERE name=@oldname", conn))
{
cmd.Parameters.AddWithValue("@NewName", textBox1.Text);
cmd.Parameters.AddWithValue("@NewAge", textBox2.Text);
cmd.Parameters.AddWithValue("@NewPhone", textBox3.Text);
cmd.Parameters.AddWithValue("@NewAddress", textBox4.Text);
cmd.Parameters.AddWithValue("@oldname", orderId);
try
{
int rows = cmd.ExecuteNonQuery();
MessageBox.Show("Updated Successfully");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
BindGrid();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
button4.Enabled=true;
}
但是在我单击“更新”按钮后,单击与行e.ColumnIndex
对应的按钮列始终返回0.
我做错了什么?
更新:
请参阅截图
答案 0 :(得分:3)
Rahul的答案会起作用但看起来orderId = (string)dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value;
应该被修改为获得Cells[1].Value
而不是0,因为0是从第二次开始的按钮单元格。但是第一次你需要使用0然后按钮位于索引4.尝试以下更改。
据我所知,问题是在更新之后,你再次调用BindGrid并且因为flag为true它只是为网格再次设置数据源而不再生成按钮因此按钮被移动到索引0后跟数据表列
另一种解决方法是修改你的代码
dataGridView1.Columns.Insert(0, uninstallButtonColumn);
dataGridView1.Columns[0].DisplayIndex = 4;
然后在CellClick
事件中检查e.ColumnIndex == 0
。这有助于您在所需位置显示按钮,并始终点击工作,因为columnindex永远不会更改。
答案 1 :(得分:0)
您可以在不比较button index
:
index
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
//TODO - Button Clicked - Execute Code Here
}
}