2011-06-03 80 views
2

我的作业是在ASP.NET中,我的教授希望我删除gridview中不使用SqlDataSource的行。这可能吗?因为我认为我的教授想要让我失望,只是因为我问了一个问题而他无法回答。如何在c#中没有SqlDataSource的情况下在gridview中删除一行?

+0

NitinJS向您展示了一种解决方案,实际上您可以在没有任何回传的情况下在网格视图中删除一行:) – Maidot 2011-06-03 09:22:42

+0

您使用的是什么类型的数据源? – Magnus 2011-06-03 09:39:42

+0

男孩!你的教授确实问了一个棘手的问题。 :)顺便说一句,你问你的教授? – naveen 2011-06-03 10:18:58

回答

0

我你只是想删除的行找到行索引和简单的调用方法

datagridview.rows.removeat(rowindex); 
+2

GridView!= DataGridView – Magnus 2011-06-03 09:38:56

1

是的,你可以从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(); 
     } 
    } 

不是最好的代码,但如果你的教授要你做,你在这里。 希望这可以帮助你...

0

有一个更好的方法,而不必重新绑定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,除了在第一时间获取数据。

并告诉你的教授他是一头傲慢的猪。

相关问题