我们如何删除DataView中的行 我目前正在使用
DataView DV = new DataView();
//填充
DV.Delete(5);
但对于动态行,我们不知道dataview中任何值的行索引 就像我在数据视图中有100条记录(行) 我希望所有记录接受一个值让X(任何值) 所以我如何从Dataview或
中删除它的特定值任何建议
答案 0 :(得分:3)
答案 1 :(得分:0)
假设myRow
类型为System.Data.DataRow
,您可以这样做:
int index = DV.Table.Rows.IndexOf(myRow);
DV.Delete(index);
答案 2 :(得分:0)
您可以创建排序DataView
:
var dv = new DataView(dataTable, "id", null, DataViewRowState.CurrentRows);
然后找到行索引
var index = dv.Find(id);
if (index > -1)
dv.Delete(index);
答案 3 :(得分:0)
尝试访问 DataRow,然后应用删除。
DV.Row.Delete()
答案 4 :(得分:-1)
试试这个:
protected void gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString()))
{
// Create a command object.
SqlCommand cmd = new SqlCommand();
// Assign the connection to the command.
cmd.Connection = conn;
// Set the command text
// SQL statement or the name of the stored procedure
cmd.CommandText = "DELETE FROM Products WHERE ProductID = @ProductID";
// Set the command type
// CommandType.Text for ordinary SQL statements;
// CommandType.StoredProcedure for stored procedures.
cmd.CommandType = CommandType.Text;
// Get the PersonID of the selected row.
string strProductID = gridview1.Rows[e.RowIndex].Cells[0].Text;
// Append the parameter.
cmd.Parameters.Add("@ProductID", SqlDbType.BigInt).Value = strProductID;
// Open the connection and execute the command.
conn.Open();
cmd.ExecuteNonQuery();
}
// Rebind the GridView control to show data after deleting.
BindGridView();
}