我的作业是在ASP.NET中,我的教授希望我删除gridview
中不使用SqlDataSource
的行。这可能吗?因为我认为我的教授想要让我失望,只是因为我问了一个问题而他无法回答。如何在c#中没有SqlDataSource的情况下在gridview中删除一行?
回答
我你只是想删除的行找到行索引和简单的调用方法
datagridview.rows.removeat(rowindex);
GridView!= DataGridView – Magnus 2011-06-03 09:38:56
是的,你可以从GridView控件不使用的SqlDataSource删除行。你所要做的就是删除源代码行(无论源代码是什么...),这是绑定到你的gridview。对于这个问题
继承人的示例代码:
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();
}
}
不是最好的代码,但如果你的教授要你做,你在这里。 希望这可以帮助你...
有一个更好的方法,而不必重新绑定Gridview
和它强制呼叫SqlDataSource
。使用ViewState
。
加载Gridview时,将“数据”保存到ViewState变量中。
即:
//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;
}
所以,现在当过GridView的负载,你必须在内存用作ViewState的数据的。
如果您需要删除一行,做到这一点...
在我的例子,我使用一个搜索功能来寻找我要基于从下拉列表控制SelectValue删除,该行。你必须使用类似的东西来确定你想要删除的行。如果您想删除最后一行,请在DataTable上逐行执行ForEach,直到到达最后一行并删除!
//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;
}
}
所以你可以看到,使用ViewState的作为替代的SqlDataSource的,你能再次操纵GridView控件如你所愿,从不调用原始的SqlDataSource,除了在第一时间获取数据。
并告诉你的教授他是一头傲慢的猪。
- 1. 如何在不使用SqlDataSource,ObjectDataSource等的情况下删除gridview和底层数据源中的一行
- 2. 如何在没有约束检查的情况下删除表
- 3. Git行在没有通知的情况下被删除
- 4. 在没有constarint的情况下删除行
- 5. 如何在没有“删除”操作的情况下删除Object的属性
- 6. 如何在没有内容的情况下删除NicEdit中的空格?
- 7. ASP.NET如何在没有GridView的情况下显示数据
- 8. 如何在没有导航器的情况下删除和插入行
- 9. 在没有DataBind的情况下向GridView添加新行
- 10. 如何在没有按下c#键的情况下按下键?
- 11. 在C#2008中删除Gridview的行
- 12. RecycleView:如何在没有滚动的情况下转到一行
- 13. 如何在没有SqlDataSource的情况下将mysql连接到DevExpress ASPxScheduler
- 14. 如何在某些情况下使用EF执行删除行?
- 15. 在没有EXE的情况下删除Windows服务
- 16. 在没有GUI的情况下从nexus删除工件
- 17. SQLAlchemy:如何在不查询的情况下删除多行
- 18. 如何在这种情况下删除重复的行?
- 19. PHPpgAdmin:如何在不使用SQL的情况下删除行
- 20. 如何在不删除变量的情况下删除变量中的数据?
- 21. 如何删除所有的CSS在这种情况下属性
- 22. C#在gridview中标识行以删除
- 23. 在没有XmlDocument的情况下在C#中创建XmlNode/XmlElement?
- 24. 在没有Visual Studio的情况下在C#中嵌入资源
- 25. 如何在不删除文件的情况下从netbeans中删除项目?
- 26. 如何在不删除子表的情况下删除有约束的表?
- 27. 如何在没有CameraCaptureDialog的情况下在Windows Mobile中拍照?
- 28. 如何在没有子查询的情况下聚合一列中的行?
- 29. 如何在没有删除输入的情况下清空td的值?
- 30. 有没有办法在没有DOM的情况下写入/删除xml文件?
NitinJS向您展示了一种解决方案,实际上您可以在没有任何回传的情况下在网格视图中删除一行:) – Maidot 2011-06-03 09:22:42
您使用的是什么类型的数据源? – Magnus 2011-06-03 09:39:42
男孩!你的教授确实问了一个棘手的问题。 :)顺便说一句,你问你的教授? – naveen 2011-06-03 10:18:58