更新插入和删除gridview而不使用数据库

时间:2014-02-28 05:20:12

标签: c# asp.net gridview

我可以删除和插入,但我不知道如何选择和更新。我正在使用DataTable而我不必使用数据库来更新它

标记:

<div>
    Name <asp:TextBox ID="TxtName" runat="server" ></asp:TextBox>
    Age <asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>

    <asp:Button ID="btnInsert" Text="Insert"  runat="server" OnClick="btnInsert_Click"  />

    <asp:GridView ID="grdView" runat="server" OnRowCommand="grdView_RowCommand" OnRowDeleting="grdView_RowDeleting" OnSelectedIndexChanged="grdView_SelectedIndexChanged"   >

    <Columns>
        <asp:TemplateField>               
                <ItemTemplate>
                    <asp:Button ID="btn" runat="server" Text="Delete"  CommandName="delete" />
                </ItemTemplate>
                <EditItemTemplate>

                </EditItemTemplate>
                <%--<ItemTemplate>
                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                </ItemTemplate>--%>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button runat="server" CommandName="Change" ID="btnEdit" Text="Edit" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

代码:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static DataTable dt;
    public static int i = 1;
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        Session["datatable"] = dt;
        dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        //
        if (Session["datatable"] != null) 
        {

            dt = (DataTable)Session["datatable"];
            dr = dt.NewRow();
            dr["RowNumber"] = i;
            dr["Column1"] = TxtName.Text;
            dr["Column2"] = txtAge.Text;
            dt.Rows.Add(dr);
        }
        else
        {
             dr = dt.NewRow();
            dr["RowNumber"] = i;
            dr["Column1"] = TxtName.Text;
            dr["Column2"] = txtAge.Text;
            dt.Rows.Add(dr);
        }

        grdView.DataSource = dt;
        grdView.DataBind();
        i++;
    }

    //protected void btn_Click(object sender, EventArgs e)
    //{
    //    dt = (DataTable)Session["datatable"];
    //    if (dt.Rows.Count > 0)
    //    {

    //        dt.Rows.RemoveAt(Convert .ToInt16 ( grdView.SelectedRow) );
    //        grdView .DataSource = dt;
    //        grdView.DataBind();

    //    }
    //}

    protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            dt = (DataTable)Session["datatable"];
            if (dt.Rows.Count >= 0)
            {

                dt.Rows.RemoveAt(Convert.ToInt16(grdView.SelectedRow));
                grdView.DataSource = dt;
                grdView.DataBind();

            }
            else if (e.CommandName == "Change")
            {
                TxtName.Text = grdView .Rows[grdView .SelectedIndex].Cells[1].Text;
            }
        }
    }

    protected void grdView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        grdView.DataSource = dt;
        grdView.DataBind();
    }

    protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

2 个答案:

答案 0 :(得分:2)

来自微软的How to: Edit Rows in a DataTable

  

更新无类型数据集中的现有记录(已知行索引)   以下示例显示如何更新与前面示例相同的数据,使用表替换集合索引,以及作为字符串传递的列名称。您仍然需要知道要编辑的行的索引。

dataSet1.Tables["Customers"].Rows[4]["CompanyName"] = "Updated Company Name";
dataSet1.Tables["Customers"].Rows[4]["City"] = "Seattle";

因此,在您的示例中,如果grdView.SelectedIndex是您要更新的行索引,请像对待其他人一样设置命令,然后尝试类似下面的示例。

即。使用文本字段中的值更新所选行...

dt = (DataTable)Session["datatable"];
if (dt.Rows.Count >= 0 && grdView.SelectedIndex != -1)
{
    dt.Rows[grdView.SelectedIndex]["Column1"] = TxtName.Text;
    dt.Rows[grdView.SelectedIndex]["Column2"] = txtAge.Text;
    grdView.DataSource = dt;
    grdView.DataBind();
 }

...将使用您的文本框值来更新行。我将dt.Rows.Count >= 0留在那里因为你用其他方法执行了检查。

如果您希望命令按钮使用当前行的值填充文本字段,则需要执行相反的操作。

即。使用选定行的值填充文本字段...

dt = (DataTable)Session["datatable"];
if (dt.Rows.Count >= 0 && grdView.SelectedIndex != -1)
{
    TxtName.Text = (string)dt.Rows[grdView.SelectedIndex]["Column1"];
    txtAge.Text = (string)dt.Rows[grdView.SelectedIndex]["Column2"];
 }

答案 1 :(得分:1)

     public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
            public static DataTable dt;
            public static int i = 1;
            protected void btnInsert_Click(object sender, EventArgs e)
            {
                if (Convert.ToString(ViewState["Row"]) != "")
                {
                    dt.Rows[Convert.ToInt32(ViewState["Row"])]["Column1"] = TxtName.Text;
                    dt.Rows[Convert.ToInt32(ViewState["Row"])]["Column2"] = txtAge.Text;
                    grdView.DataSource = dt;
                    grdView.DataBind();
                    ViewState["Row"] ="";

                }
                else if(Convert.ToString(ViewState["Row"]) == "")
                {
                    Session["datatable"] = dt;
                    dt = new DataTable();
                    DataRow dr = null;
                    dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
                    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
                    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
                    //
                    if (Session["datatable"] != null)
                    {

                        dt = (DataTable)Session["datatable"];
                        dr = dt.NewRow();
                        dr["RowNumber"] = i;
                        dr["Column1"] = TxtName.Text;
                        dr["Column2"] = txtAge.Text;
                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        dr = dt.NewRow();
                        dr["RowNumber"] = i;
                        dr["Column1"] = TxtName.Text;
                        dr["Column2"] = txtAge.Text;
                        dt.Rows.Add(dr);
                    }

                    grdView.DataSource = dt;
                    grdView.DataBind();
                    i++;
                }
            }

            //protected void btn_Click(object sender, EventArgs e)
            //{
            //    dt = (DataTable)Session["datatable"];
            //    if (dt.Rows.Count > 0)
            //    {

            //        dt.Rows.RemoveAt(Convert .ToInt16 ( grdView.SelectedRow) );
            //        grdView .DataSource = dt;
            //        grdView.DataBind();

            //    }
            //}

            protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "delete")
                {
                    dt = (DataTable)Session["datatable"];
                    if (dt.Rows.Count >= 0)
                    {

                        dt.Rows.RemoveAt(Convert.ToInt16(e.CommandArgument ));
                        grdView.DataSource = dt;
                        grdView.DataBind();

                    }

                }
                else if (e.CommandName == "Change")
                {
                    dt = (DataTable)Session["datatable"];
                  //  TxtName.Text = (string)dt.Rows[grdView.SelectedIndex]["Column1"];
                    TxtName.Text = grdView.Rows[Convert.ToInt32(e.CommandArgument)].Cells[4].Text;
                    txtAge.Text = grdView.Rows[Convert.ToInt32(e.CommandArgument)].Cells[5].Text;
                    ViewState["Row"] = e.CommandArgument;
                    if (dt.Rows.Count >= 0  )
                    {

                        //dt.Rows[grdView.SelectedIndex]["Column1"] = TxtName.Text;
                        //dt.Rows[grdView.SelectedIndex]["Column2"] = txtAge.Text;
                        grdView.DataSource = dt;
                        grdView.DataBind();
                    }

                }
            }

            protected void grdView_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                grdView.DataSource = dt;
                grdView.DataBind();
            }

            protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
            {
                TxtName.Text = grdView.SelectedRow.Cells[2].Text;
            }

            //protected void btnUpdate_Click(object sender, EventArgs e)
            //{
            //    dt.Rows[Convert.ToInt32(ViewState["Row"])]["Column1"] = TxtName.Text;
            //    dt.Rows[Convert.ToInt32(ViewState["Row"])]["Column2"] = txtAge.Text;
            //    grdView.DataSource = dt;
            //    grdView.DataBind();
            //}


        }






 <div>
    Name    <asp:TextBox ID="TxtName" runat="server" ></asp:TextBox>
       Age<asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
        <asp:Button ID="btnInsert" Text="Save"  runat="server" OnClick="btnInsert_Click"  />
      <%--  <asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="btnUpdate_Click" />--%>
    <asp:GridView ID="grdView" runat="server" OnRowCommand="grdView_RowCommand" OnRowDeleting="grdView_RowDeleting" 
>
        <Columns>
            <asp:TemplateField>

                <ItemTemplate>
                    <asp:Button ID="btn" runat="server" Text="Delete"  CommandName="delete" CommandArgument='<%# Container.DataItemIndex %>' />
            </ItemTemplate>

         <%--   <ItemTemplate>
                <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            </ItemTemplate>--%>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" CommandName="Change" ID="btnEdit" Text="Edit" CommandArgument='<%# Container.DataItemIndex %>'  />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>

        </asp:TemplateField>
    </Columns>
</asp:GridView>
</div>