Gridview添加/删除行

时间:2016-01-22 21:06:58

标签: c# asp.net vb.net gridview

如何添加,删除和编辑“包含文本框”的多行到Gridview

不插入数据库

我试过

String projectName = "C:/Users/soapui-project.xml" 
List<String> wsdlFiles = ["http://abc.zxc.io/wsdl/v1/?appname=abc version=1.0"] 
UpdateWsdls updateWsdl = new UpdateWsdls(projectName)

并删除

Gridview1.rows.add(datarow)

但没有检测到选定的行

1 个答案:

答案 0 :(得分:1)

您可以在下面的代码中使用此方法。在显示的addRow方法中,插入了一个新的网格行。逻辑是我们需要将网格重新绑定到包含原始行和新空行的新数据源。您可以使用类似的方法进行删除(创建一个新的数据源,排除已删除的行,然后重新绑定网格)。

删除时使用方法deleteRow。我假设您在网格行中有一个id为chkDelete的复选框控件,检查时表示需要删除该行。您可以使用deleteRow

的方法同时删除多行

如果您使用以下两种方法添加行并删除行,则自动编辑的文本框将保留其新值,即editing would then be automatically taken care of

做出的假设另外,我假设除了一个复选框,网格行中还有3个文本框。因为有3个文本框,所以DataTable在下面的方法中创建的应该包含3个这三个文本框的列,这些列应该是字符串类型。

添加一行

protected void addRow()
{
    DataTable dt = new DataTable(); 
    //add code to create columns for this data table
    //only create columns for textbox data
    dt.Columns.Add("Column1", typeof(string));
    dt.Columns.Add("Column2", typeof(string));
    dt.Columns.Add("Column3", typeof(string));
    DataRow dr = null;

    //build a data source of existing rows
    foreach (GridViewRow gridRow in grid1.Rows)
    {
        dr = dt.NewRow();

        //set only text box values in new data source
        //so checkbox column for row selection will be ignored
        TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox;
        TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox;
        TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox;

        dr[0] = txtColumn1.Text;
        dr[1] = txtColumn2.Text;
        dr[2] = txtColumn3.Text;

        dt.Rows.Add(dr); 
    }

    //create the row in data sourec for the new grid row
    dr = dt.NewRow(); 
    dt.Rows.Add(dr);

    //bind the grid view, which will now show you the new added row in addition to original rows
    grd.DataSource = dt; 
    grd.DataBind();
}

删除行

protected void deleteRow()
{
    DataTable dt = new DataTable(); 
    //add code to create column for this data table
    //only set column for textbox columns
    dt.Columns.Add("Column1", typeof(string));
    dt.Columns.Add("Column2", typeof(string));
    dt.Columns.Add("Column3", typeof(string));

    //build a data source of existing rows
    foreach (GridViewRow gridRow in grid1.Rows)
    {
        //get whether the checkbox for deleting row is checked or not
        CheckBox chkDelete = gridRow.FindControl("chkDelete") as CheckBox;
        //do not add original row if it was checked to be deleted
        if(!chkDelete.Checked)
         {
           dr = dt.NewRow();

           //set only text box values in new data source
           //so checkbox column for row selection will be ignored
           TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox;
           TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox;
           TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox;

           dr[0] = txtColumn1.Text;
           dr[1] = txtColumn2.Text;
           dr[2] = txtColumn3.Text;

           dt.Rows.Add(dr); 
       }
    }

    //bind the grid view, which will now NOT have the deleted rows
    grd.DataSource = dt; 
    grd.DataBind();
}