Ken Hollis
Ken Hollis

Reputation: 71

Add data from Textbox to GridView on button click

I have 1 Dropdownlist and 3 Textboxes and I want to add data from textboxes and after a button click they will be added to GridView

<asp:DropdownList ID="ddlClassification" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>Sample 1</asp:ListItem>
<asp:ListItem>Sample 2</asp:ListItem>
<asp:ListItem>Sample 3</asp:ListItem>
</asp:DropdownList>

<asp:TextBox ID="tbCost" runat="server" Width="100" ></asp:TextBox>
<asp:TextBox ID="tbAccumulate" runat="server" Width="100" ></asp:TextBox>
<asp:TextBox ID="tbNRV" runat="server" Width="100" ></asp:TextBox>

<asp:Button ID="btnAdd" runat="server" Text="ADD" onclick="btnAdd_Click" />

and this is What I have in code behind

public partial class SetUP : System.Web.UI.Page
{
    DataRow dr;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("Classification");
        dt.Columns.Add("Cost");
        dt.Columns.Add("Accumulate");
        dt.Columns.Add("NRV");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        dr = dt.NewRow();
        dr["Classification"] = ddlClassification.Text;
        dr["Cost"] = tbCost.Text;
        dr["Accumulate"] = tbAccumulate.Text;
        dr["NRV"] = tbNRV.Text;
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

I have done this once but now I am getting error on GridView1.DataBind(); can someone help me in my problem

Upvotes: 1

Views: 9367

Answers (1)

voddy
voddy

Reputation: 1000

when you add column do like this

 dt.Columns.Add(new DataColumn("Classification", typeof(string)));

should help you

Upvotes: 1

Related Questions