You must Sign In to post a response.
  • Category: ASP.NET

    Adding value to datable and bind to gridview

    In run mode as follows

    Major code AMC(Dropdownlist) Minor code AFF(dropdownlist) Button(Add)

    Add button code as follows
    protected void btnadd_Click(object sender, EventArgs e)
    Dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Major Code", typeof(string)),
    new DataColumn("Minor Code", typeof(string))});
    Dt.Rows.Add(ddlmjrcode.SelectedItem.ToString(), ddlminorcode.SelectedItem.ToString());

    gvmajormaster.DataSource = Dt;

    When i click the add button i want to add the AMC and AFF in first row in gridview and second row AMC and BMC for that how can i do in using c#.

    Excepted output in Gridview as follows
    Major code Minor code

    But my output in gridview as follows
    Major code Minor code

    When i add the second minor code in gridview in first row Minor code gets replaced.

    for that how can i do in using c#.
  • #762207

    You can follow this Code

    Client Side

    <asp:DropDownList ID="DDL1" runat="server">
    <asp:ListItem Text="AMC">AMC</asp:ListItem>
    <asp:DropDownList ID="DDL2" runat="server">
    <asp:ListItem Text="AFF">AFF</asp:ListItem>

    <asp:GridView ID="Grd1" runat="server"></asp:GridView>

    Server Side

    DataTable dt = new DataTable();
    DataRow dr;
    dr = dt.NewRow();
    dr[0] = "AMC";
    dr[1] = "AFF";

    Grd1.DataSource = dt;

    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762213
    Hello Rao,

    Refer the below code :

    HTML Markup :

    <html xmlns="" >
    <head runat="server">
    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="AMC" Value="AMC">
    <asp:ListItem Text="AMC" Value="AMC">

    <asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Text="AFF" Value="AFF">
    <asp:ListItem Text="BMC" Value="BMC">

    <asp:Button ID="ButtonAdd" runat="server" Text="Add"
    onclick="ButtonAdd_Click" />

    <br /><br />

    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333">
    <RowStyle BackColor="#E3EAEB" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#7C6F57" />
    <AlternatingRowStyle BackColor="White" />

    Code - behind :

    using System.Data;

    protected void ButtonAdd_Click(object sender, EventArgs e)
    DataTable dt = new DataTable();
    dt.Columns.Add("Major Code");
    dt.Columns.Add("Minor Code");

    dt.Rows.Add(DropDownList1.Items[0].Value, DropDownList2.Items[0].Value);
    dt.Rows.Add(DropDownList1.Items[1].Value, DropDownList2.Items[1].Value);

    GridView1.DataSource = dt;

    See the attached image. It's an output i have got by using above mentioned code.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762215

    As per your code, it's gets inserted only 1 record because you are adding single record into table. If you want to insert 2 records then you need to create one more row as DotNetDeveloper and Nirav said, could you please confirm one thing whether is this static data or dynamic data.? If that is static you can follow above thread if that is dynamic you need to elaborate it more. Before ask a question try to find solution by your own, put a breakpoint and check line by line then only you can understand the issue..

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

Sign In to post your comments