gridview中的数据表 [英] Data Table in gridview

查看:59
本文介绍了gridview中的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在gridview中创建了一个数据表,但是当单击addnewrow按钮时,gridview被禁用了,下面是我的代码和aspx ..

Hi,

I have created a datatable inside gridview but when click on addnewrow button then gridview is disabled follwing is my code and aspx..

private void createtable()
        {

            try
            {
                DataTable dt = new DataTable();
                DataColumn dc1 = new DataColumn("Row Number",typeof(string));
                DataColumn dc2 = new DataColumn("product_name",typeof(string));
                DataColumn dc3 = new DataColumn("product_rate",typeof(string));
             

                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
              
                DataRow dr = dt.NewRow();
                dr["Row Number"]=1;
                dr["Product_Name"] = string.Empty;
                dr["Product_rate"] = string.Empty;
                dt.Rows.Add(dr);
                         

                ViewState["currentTable"] = dt;

                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.Visible=true;
            }
            catch (Exception)
            {
                throw;
            }

        
        }

 protected void AddNewItem_Click(object seneder, EventArgs e)
      {
          addNewRow();
      
      }
      private void addNewRow()
      {
          int rowIndex = 0;
          if (ViewState["currentTable"] != null)
          {
              DataTable dtCurrentTable = (DataTable)ViewState["currentTable"];
              DataRow dtCurrentRow = null;
              if (dtCurrentTable.Rows.Count > 0)
              {
                  for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                  {
                      TextBox tb1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtItem");
                      TextBox tb2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtRate");
                      dtCurrentRow = dtCurrentTable.NewRow();
                      dtCurrentRow["Row Number"] = i + 1;
                      dtCurrentTable.Rows[i - 1]["Product_Name"] = tb1.Text;
                      dtCurrentTable.Rows[i - 1]["Product_rate"] = tb2.Text;
                      rowIndex++;
                  }
                  dtCurrentTable.Rows.Add(dtCurrentRow);
                  ViewState["currentTable"] = dtCurrentTable;
                  GridView1.DataBind();
              }
          }
          else
          {
              Response.Write("ViewState is null");
          }

         setpreviousdata();
      
      }

      private void setpreviousdata()
      {

          int rowIndex = 0;

          if (ViewState["CurrentTable"] != null)
          {
              DataTable dt1 = (DataTable)ViewState["CurrentTable"];
              if (dt1.Rows.Count > 0)
              {
                  for (int i = 0; i < dt1.Rows.Count; i++)
                  {
                      TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtItem");
                      TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtRate");

                      box1.Text = dt1.Rows[i]["Product_Name"].ToString();
                      box2.Text = dt1.Rows[i]["Product_rate"].ToString();
                    
                      rowIndex++;
                  }
              }
          }
      }



aspx



aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
            <asp:BoundField HeaderText="Row Number" DataField="Row number" />
                <asp:TemplateField HeaderText="Item">
                  
                <ItemTemplate>
                <asp:TextBox ID="txtItem" runat="server" />
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                       <asp:TextBox ID="txtRate" runat="server" />
                    </ItemTemplate>
               <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                <asp:Button ID="btnAddNewItem" runat="server" OnClick="AddNewItem_Click" Text="Add New Item" />
                </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



谢谢,



thanks

推荐答案

好,您没有正确实现此功能.

看看这篇文章:可编辑Gridview [
Well, you are not implementing the feature correctly.

Have a look at this article: Editable Gridview[^]

This explains how you can make your grid editable and have new rows added to it.


这篇关于gridview中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆