How Delete/remove row/rows from dataview (without using Dataview. Delete )
How we delete The row from DataView i currently using
DataView DV = new DataView();
//populate
DV.Delete(5);
but for the dynamic rows we don't know row index for any value in dataview like i have 100 record in data view (rows) where i want all record accept one value lets X(any开发者_如何转开发 value) so how i delete it's specific value from Dataview or
any suggestion
In case you want to hide the rows, a RowFilter could solve your problem.
EDIT
If you want to remove them from the DataTable use the Select method of the DataTable and remove the DataRows that have been found.
Asssuming myRow
is of type System.Data.DataRow
you can do:
int index = DV.Table.Rows.IndexOf(myRow);
DV.Delete(index);
You can create a sorted DataView
:
var dv = new DataView(dataTable, "id", null, DataViewRowState.CurrentRows);
and then find row index
var index = dv.Find(id);
if (index > -1)
dv.Delete(index);
Try accessing the DataRow, and then apply the delete.
DV.Row.Delete()
Try this:
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();
}
精彩评论