如何在Gridview中添加新行? [英] how to add new row into my Gridview ?

查看:87
本文介绍了如何在Gridview中添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:Button ID="btn_AddNew" runat="server" CssClass="buttonNormal"
                    Text="Add New" onclick="btn_AddNew_Click"  />










<asp:GridView ID="gv_AparamType" runat="server" AutoGenerateColumns="False"

                                CssClass="gv" EmptyDataText="No Records found"

                                OnRowCancelingEdit="gv_AparamType_RowCancelingEdit"

                                OnRowEditing="gv_AparamType_RowEditing" OnRowUpdating="gv_AparamType_RowUpdating"

                    onselectedindexchanged="gv_AparamType_SelectedIndexChanged" >
                                <Columns>
                                    <asp:TemplateField HeaderText="SL NO">

                                        <ItemTemplate>
                                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("SlNo") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="MODULE">

                                        <ItemTemplate>
                                            <asp:Label ID="Lbl_Module" runat="server" Text='<%# Bind("Module") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="PARAMETERTYPE">

                                        <ItemTemplate>
                                            <asp:Label ID="lbl_PType" runat="server" Text='<%# Bind("ParamType") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="PARAMETERVALUE" itemstyle-width="150">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txt_PValue" runat="server" Text='<%# Bind("ParamValue") %>' TextMode="MultiLine"></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label1" runat="server"  Text='<%# Bind("ParamValue") %>'></asp:Label>
                                        </ItemTemplate>
                                       <%-- <ItemStyle Width="1000px" />--%>
                                    </asp:TemplateField>

                                    <asp:CommandField HeaderText="Edit" ShowEditButton="True"  />
                                </Columns>
                            </asp:GridView>



这里我有一个按钮添加Ne w我在这个gridview中有一个gridveiw我直接从我的表格中的数据中绑定数据,然后在我的gridview列中单击该编辑链接时有一个编辑链接第3列数据存储到textBox并且我编辑和更新。但是



我的要求是当我点击addNew时我想要添加具有文本框的新行每个节点的节点进入我同样的格子视图请任何人帮助我吗?

代码allso我想要代码onclick addNew_button请


here i have one button "Add New" and I have one "gridveiw" in this gridview i directtly binddata from what ever data is there in my table,,and after that in have one "edit link" is there when i click the that edit link in my gridview column then the 3 rd column data store into "textBox" and i edit and updating.but

my requirement is when i click "addNew" i want add new row that has textbox node for each column into my same "gried view" please can any one help me?
with code allso i want code for"onclick addNew_button" please please

推荐答案

请尝试如下。



注意:这只是一个示例。请根据您的具体情况进行更改。



Please try is as below.

Note : This is just a sample.Change it according to your situation.

protected void Button1_Click(object sender, EventArgs e)
   {
       DataTable dt = new DataTable();
       DataColumn dc = new DataColumn();

       if (dt.Columns.Count == 0)
       {
           dt.Columns.Add("PayScale", typeof(string));
           dt.Columns.Add("IncrementAmt", typeof(string));
           dt.Columns.Add("Period", typeof(string));
       }

       DataRow NewRow = dt.NewRow();
       NewRow[0] = TextBox1.Text;
       NewRow[1] = TextBox2.Text;
       dt.Rows.Add(NewRow); 
       GridView1.DataSource = dt;
       GridViewl.DataBind();
   }





点击此处查看更多信息: 在绑定C#,ASP.net后在gridview中添加新行



在按钮上向GridView添加新行在ASP.Net中单击


绑定到网格的数据表,假设数据表是dtGrid。将其保存在ViewState / Session中

下面我提供了一个示例代码。我希望这给你一个构思的基础......

这段代码将添加空行...

The datatable bound to the grid, assume the datatable is dtGrid. Save that in a ViewState/Session
Below I have provided a sample code. I hope this gives you an idea to build on...
This code will add empty row...
OnAddNew_Click()
{
dtGrid=(DataTable)ViewState["dt"];
DataRow dr = dtGrid.NewRow();
dr[0]=""
dr[1]=""
dr[2]=""
dr[3]=""
dr[4]=""
dtGrid.Rows.Add(dr);
gridView.DataSource=dtGrid;
gridView.DataBind();
}







希望这有帮助




Hope this helps


这篇关于如何在Gridview中添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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