gridView将选中行插入表.net
时间: 2023-08-16 15:07:16 浏览: 130
在 ASP.NET 中,可以使用 GridView 控件来显示和编辑数据。要将选定行插入表中,可以采用以下步骤:
1. 在 GridView 控件中,设置 AutoGenerateSelectButton 属性为 true,以便在每一行生成一个选择按钮。
2. 在代码中,订阅 GridView 的 SelectedIndexChanged 事件。
3. 在事件处理程序中,获取选定行的数据,并插入到表中。
下面是一个示例代码:
```C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// 获取选定行的数据
GridViewRow row = GridView1.SelectedRow;
string name = row.Cells[0].Text;
int age = int.Parse(row.Cells[1].Text);
string gender = row.Cells[2].Text;
// 将数据插入到表中
string connectionString = "your_connection_string";
SqlConnection connection = new SqlConnection(connectionString);
string sql = "INSERT INTO YourTable(Name, Age, Gender) VALUES(@Name, @Age, @Gender)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Age", age);
command.Parameters.AddWithValue("@Gender", gender);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
```
注意,这只是一个示例代码,需要根据实际情况进行修改。
阅读全文
相关推荐


















