简体   繁体   English

Gridview添加/删除行

[英]Gridview Add/delete rows

How can I ADD, Delete and Edit multiple rows "containing textboxes" to Gridview 如何将“包含文本框”的多行添加,删除和编辑到Gridview

Without inserting to DB 无需插入数据库

I tried 我试过了

Gridview1.rows.add(datarow)

And for delete 并删除

Gridview1.rows.remove(datarow)

but without detecting selected row 但没有检测到选定的行

You can use the approach in code below for this. 您可以在下面的代码中使用此方法。 In the addRow method shown, a new grid row is inserted. 在所示的addRow方法中,插入了一个新的网格行。 The logic is that we need to re-bind the grid to a new data source that contains the original rows plus a new empty row. 逻辑是我们需要将网格重新绑定到包含原始行和新的空行的新数据源。 You can use a similar approach for delete ( create a new data source with deleted rows excluded and then rebind the grid). 您可以使用类似的方法进行删除(创建一个排除已删除行的新数据源,然后重新绑定网格)。

When deleting use the method deleteRow . 删除时,使用方法deleteRow I have assumed that you have a check box control with an id of chkDelete in the grid row, which when checked would mean the row needs to be deleted. 我假设您在网格行中有一个ID为chkDelete复选框控件,这在选中时意味着该行需要删除。 You can delete multiple rows at the same time using the method of deleteRow . 您可以使用deleteRow方法同时删除多行。

If you use the below two methods for adding a row and deleting row(s) then automatically your edited text boxes would have their new values retained always ie editing would then be automatically taken care of . 如果您使用以下两种方法添加行和删除行,则编辑后的文本框将自动始终保留其新值,即editing would then be automatically taken care of

Assumptions made : Also, I have assumed that there are 3 text boxes in grid row in addition to a check box. 进行的假设此外,我还假设除了复选框之外,网格行中还有3个文本框。 Because there are 3 textboxes, so the DataTable being created in methods below should contain 3 columns for these 3 textboxes and these columns should be of string type. 因为有3个文本框,所以在下面的方法中创建的DataTable应对这3个文本框包含3列,并且这些列应为字符串类型。

Add a Row 添加一行

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();
}

Delete Row(s) 删除行

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM