How to delete a row in a gridview without a SqlDataSource in c#?
My homew开发者_开发技巧ork is in ASP.NET and my prof wants me to delete a row from a gridview
that doesn't use a SqlDataSource
. Is this possible? Because I think my prof wants to fail me just because I asked a question and he wasn't able to answer it.
Yes you can delete a row from gridview that doesn't use sqldatasource. All you have to do is delete the row from the source (whatever the source is...), that is bind to your gridview.
heres sample code for the issue:
public static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = "column1cell";
dr["Column2"] = "column2cell";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (dt.Rows.Count > 0)
{
dt.Rows.RemoveAt(0);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
not the best code, but if your prof wants you to do, here you are. hope this helps you...
I you Just want to delete the row find the row index and the simply call the method
datagridview.rows.removeat(rowindex);
There is a better way without having to rebind the Gridview
and it forcing a call to the SqlDataSource
.
Use ViewState
.
When you load the Gridview, save the "data" into a ViewState variable.
ie:
//ok let's load the gridview with data as normal and display it
//'sdsClasses' is the SQL data source
gvStudents.DataSourceID = "sdsClasses";
gvStudents.DataSource = null; // Null out the source, as we have a SourceID instead
gvStudents.DataBind(); //load the gridview and display it
//save the data in a viewstate for later use
DataView dvClasses = (DataView)sdsClasses.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
if (dv != null)
{
dt = dvClasses.ToTable();
ViewState["gv"] = dt;
}
So now when ever the Gridview loads, you have the data its used in memory as a ViewState.
If you need to delete a row, do this ...
In my example I am using a search feature to look for the row I want to delete, based on a SelectValue from a dropdownlist control. You'll have to use something like that to pin-point the row you want to delete. If you wanted to delete the last row, then do a ForEach on the DataTable, row-by-row until you get to the last row and delete!
//Load the dataview that was already saved in the ViewState
DataTable dt = (DataTable)ViewState["gv"];
//find the student in the datatable, row by row
bool found = false;
bool wsAtt = false; //flag to indicate if the student is already in the roll or not saved yet (ie: sdsClasses recordset)
foreach (DataRow dr in dt.Rows)
{
//compare studentID in the datatable with the selected value of the student to delete
//check that the field has TECNQ studentIDs otherwise use the 2nd cell in the row
if (dr[0].ToString().Contains("NQ"))
found = (found || dr[0].ToString() == ddlRemoveStudents.SelectedValue);
else
{
found = (found || dr[1].ToString() == ddlRemoveStudents.SelectedValue);
wsAtt = true;
}
//he should!
if (found)
{
//remove the row to the datatable
dt.Rows.Remove(dr);
//Bind the grid view to the datatable and refresh
gvStudents.DataSource = dt;
gvStudents.DataSourceID = null; // Null out the id, we have a source
gvStudents.DataBind();
//update the viewstate with the new amount of rows
ViewState["gv"] = dt;
}
}
So you can see, using a ViewState as a replacement to the SqlDataSource, you're able to manipulate the Gridview as you wish and never call the original SqlDataSource again, except the first time to get the data.
And tell your professor he's an arrogant pig.
精彩评论