-1

How to Add Textfield column into Gridview in asp.net C# ?

I need to add a Text field Column at last of the Gridview, I create Data Table dynamically like this. I need to add text fields for comisson column

DataTable dt = new DataTable(); // create data table
            dt.Columns.Add("TransDate"); // create column 1
            dt.Columns.Add("ExHouse");// create column 2           
            dt.Columns.Add("RemName");
            dt.Columns.Add("Currency");
            dt.Columns.Add("Amount");
            dt.Columns.Add("LKRAmount");
            //dt.Columns.Add(new DataColumn("Comission", typeof(string))); 
            dt.Columns.Add("Comission");
            dt.Columns.Add("ValueDate");

            DataRow dr = dt.NewRow(); // Create new Row
            TextBox comi=new TextBox();
            dr["TransDate"] = RemittanceToSelectedTable.TransDate; 
            dr["ExHouse"] = RemittanceToSelectedTable.ExHouse; //add data to coolumn no2
            dr["RemName"] = RemittanceToSelectedTable.RemName;
            dr["Currency"] = RemittanceToSelectedTable.Currency;
            dr["Amount"] = RemittanceToSelectedTable.Amount;
            dr["LKRAmount"] = RemittanceToSelectedTable.LKRAmount;

            dr["ValueDate"] = RemittanceToSelectedTable.ValueDate;
            dr["Comission"] = comi;

            aa.Add((string)dr["TransDate"]); 
            ab.Add((string)dr["ExHouse"]);
            ac.Add((string)dr["RemName"]);
            ad.Add((string)dr["Currency"]);
            ae.Add((string)dr["Amount"]);
            af.Add((string)dr["LKRAmount"]);
          //  ah.Add((string)dr["Comission"]);
            ag.Add((string)dr["ValueDate"]); 


            for (i = 0; i < ag.Count; i++)
            {
                dt.Rows.Add(aa[i], ab[i], ac[i], ad[i], ae[i], af[i], ag[i]); // add rows to data table
            }

            return dt;

Thanks !

4

3 回答 3

3

感谢回复...这是最终的答案... GridView 一定是这样的

        <Columns>
          <%--<asp:BoundField HeaderText="Comission" DataField="Comission"></asp:BoundField>--%>
         <%--<asp:TextBox ID="txtChildren" Text="assan" runat="server"  Width="25px"></asp:TextBox>--%>

            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="TransDate" HeaderText="TransDate" />
            <asp:BoundField DataField="ExHouse" HeaderText="ExHouse" />
            <asp:BoundField DataField="RemName" HeaderText="RemName" />
            <asp:BoundField DataField="Currency" HeaderText="Currency" />
            <asp:BoundField DataField="Amount" HeaderText="Amount" />
            <asp:BoundField DataField="LKRAmount" HeaderText="LKRAmount" />

        <asp:TemplateField HeaderText="Comission">
        <ItemTemplate>
        <asp:TextBox ID="txtTest" runat="server"/>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField HeaderText="ValueDate" DataField="ValueDate"></asp:BoundField>

        </Columns>


    </asp:GridView>

和数据表应该是这样的

        DataTable dt = new DataTable();

        dt.Columns.Add("TransDate"); // create column 1
        dt.Columns.Add("ExHouse");// create column 2           
        dt.Columns.Add("RemName");
        dt.Columns.Add("Currency");
        dt.Columns.Add("Amount");
        dt.Columns.Add("LKRAmount");
        dt.Columns.Add("Comission");           
        dt.Columns.Add("ValueDate");

        DataRow dr = dt.NewRow(); // Create new Row

        dr["TransDate"] = RemittanceToSelectedTable.TransDate; //add data to coolumn no1
        dr["ExHouse"] = RemittanceToSelectedTable.ExHouse; //add data to coolumn no2
        dr["RemName"] = RemittanceToSelectedTable.RemName;
        dr["Currency"] = RemittanceToSelectedTable.Currency;
        dr["Amount"] = RemittanceToSelectedTable.Amount;
        dr["LKRAmount"] = RemittanceToSelectedTable.LKRAmount;
        dr["Comission"] = 0;
        dr["ValueDate"] = RemittanceToSelectedTable.ValueDate;


        aa.Add((string)dr["TransDate"]); 
        ab.Add((string)dr["ExHouse"]);
        ac.Add((string)dr["RemName"]);
        ad.Add((string)dr["Currency"]);
        ae.Add((string)dr["Amount"]);
        af.Add((string)dr["LKRAmount"]);
        ah.Add("0");
        ag.Add((string)dr["ValueDate"]); // 


        for (i = 0; i < ag.Count; i++)
        {
            dt.Rows.Add(aa[i], ab[i], ac[i], ad[i], ae[i], af[i], ah[i], ag[i]); 
        }



        return dt;
于 2012-08-01T16:29:33.997 回答
1

您可以添加模板字段并将文本框拖放到 ItemTemplate 中,如下所示:

<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
于 2012-08-01T04:54:08.710 回答
0

要将行添加到DataTable您可以使用的。

DataRow dr = dt.NewRow(); // Create new Row
dr["TransDate"] = RemittanceToSelectedTable.TransDate; 
dr["ExHouse"] = RemittanceToSelectedTable.ExHouse; //add data to coolumn no2
dr["RemName"] = RemittanceToSelectedTable.RemName;
dr["Currency"] = RemittanceToSelectedTable.Currency;
dr["Amount"] = RemittanceToSelectedTable.Amount;
dr["LKRAmount"] = RemittanceToSelectedTable.LKRAmount;
dr["ValueDate"] = RemittanceToSelectedTable.ValueDate;
dr["Comission"] = 0; //default value for commission
dt.Rows.Add(dr);

然后为了显示您的佣金文本框,您无需在数据中创建文本框。那就是将要显示的控件。您的数据将是文本、整数、浮点数。

<asp:BoundField HeaderText="Comission" DataField="Comission"></asp:BoundField>
于 2012-08-01T05:21:33.530 回答