1

我的 datagridview 中有一个组合框列,其中我添加了三个项目插入、更新和删除现在我想选择一个项目并执行相应的操作。

但是当我执行此错误时,显示输入字符串的格式不正确,可能是我无法选择相对于该组合​​框行的确切行。有人帮忙吗?

这是我的 datagridview 的组合框 SelectedIndexChanged 事件的代码

ComboBox cmb = (ComboBox)sender;

if (cmb.SelectedItem.ToString() == "insert")
{
    con = new SqlConnection(@"Data Source=krishna-PC\SQLEXPRESS;Initial Catalog=dbnew;Integrated Security=True");

    int a = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].ToString());
    string b = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString();
    string c = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Value.ToString();
    com = new SqlCommand("insert into mytable values(@a,@b,@c,@d)", con);
    com.Parameters.AddWithValue("@a", a);
    com.Parameters.AddWithValue("@b", b);
    com.Parameters.AddWithValue("@c", c);
    com.Parameters.AddWithValue("@d", "govind");
    con.Open();
    com.ExecuteNonQuery();
        MessageBox.Show("Data has been inserted");
 }
4

2 回答 2

1

只需修复单元格 1 的行,您忘记了 .Value 属性:

int a = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString());
于 2013-08-12T03:09:13.900 回答
0

而不是使用“值”使用“文本”.Like..

int a = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Text.ToString());
string b = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Text.ToString();
string c = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Text.ToString();
于 2013-08-12T04:16:37.470 回答