Click here to Skip to main content
15,883,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have some 3 dropdown list and two textboxes.and the value from these controls i want to dispalay in gridview , every time when i change the values of these controls.it should be added to the gridview.

and after adding max 10 records in gridview i want them all to be inserted into the database at once...

please gv me some suggestions. please


Its very urgent.
Posted
Updated 12-Mar-12 2:09am
v2
Comments
Herman<T>.Instance 12-Mar-12 8:11am    
both gridview and database work row based, so 10 at once is an illusion.
so after 10 rows you need a loop to have them 1 by 1 in your db.
Are you using a List<> object or an ArrayList() object? You can bind that to the gridview.
ashu1810 12-Mar-12 8:19am    
hithanks for the reply.
i am not using any <list>
i am just selecting the values from the dropdownlist and i want that values to be displayed in a gridview. (row by row)
and after adding some rows i want to insert them into a database.

plz advice me

1 solution

Hi ,

I tried some code for you check this once,

This is just for sample to get you idea how to do it .in this example i just used session variable to store entered data .

ASP.NET
<form id="form1" runat="server">
  <table width="100%">
    <tr>
       <td>First Name</td>
       <td></td>
       <td>
           <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
    </tr>
    <tr>
      <td>Last Name</td>
      <td></td>
      <td>    <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
    </tr>
    <tr>
      <td colspan="3">
          <asp:button id="Button1" runat="server" text="Add" onclick="Button1_Click" xmlns:asp="#unknown" />
      </td>
    </tr>
  </table>
  <br />
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
</asp:gridview>

</form>


after this you've to write some code

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("fname");
        dt.Columns.Add("lname");
        DataRow dr = dt.NewRow();
        dr[0] = "Murali";
        dr[1] = "krishna";
        dt.Rows.Add(dr);

        Session["TempData"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = (DataTable)Session["TempData"];

    DataRow dr = dt.NewRow();
    dr[0] = TextBox1.Text;
    dr[1] = TextBox2 .Text ;
    dt.Rows.Add(dr);
    Session["TempData"] = dt;
    GridView1.DataSource = (DataTable)Session["TempData"];
    GridView1.DataBind();
}


Here i'm just made this code for testing you've to modify this to fullfill your requiremens

All the Best
 
Share this answer
 
Comments
ashu1810 13-Mar-12 4:25am    
thanks for the code
i just tried it like this

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("SFOID");
dt.Columns.Add("OUTLETID");
dt.Columns.Add("FSMID");
dt.Columns.Add("DATE");
dt.Columns.Add("IMEI");

DataRow dr = dt.NewRow();
dr[0] = "sfo1";
dr[1] = "outlet1";
dr[2] = "fsm1";
dr[3] = "2/08/2012";
dr[4] = "111";
dt.Rows.Add(dr);

Session["TempData"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void insert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)Session["TempData"];

DataRow dr = dt.NewRow();
dr[0] = ddl_sfo.SelectedValue.ToString() ;
dr[1] = ddl_outletid.SelectedValue.ToString();
dr[2] = ddl_fsmid.SelectedValue.ToString();
dr[3] = Txtdate.Text;
dr[4] = txtimei.Text;
dt.Rows.Add(dr);
Session["TempData"] = dt;
GridView1.DataSource = (DataTable)Session["TempData"];
GridView1.DataBind();


}
it is not working properly. whenever i clik on insert button only first column is getting incremented and the other colums are empty .even after resrehing the page the the row is getting added and the first column is incementing by 1

it is not displaying the selected values in gridvew plz help me.....
Muralikrishna8811 13-Mar-12 5:21am    
Hi can you provide design code?
Muralikrishna8811 13-Mar-12 5:29am    
Hi same thing working for me with dropdownlists


check dropdownlist control has value property or not...........
ashu1810 13-Mar-12 5:54am    
here is the design code

<asp:DropDownList ID="ddl_sfo" runat="server"
DataSourceID="SqlDataSource1" DataTextField="FIRST_NAME"
DataValueField="SFO_ID" Height="17px"
Width="89px"
onselectedindexchanged="ddl_sfo_SelectedIndexChanged">


<asp:DropDownList ID="ddl_outletid" runat="server"
DataSourceID="SqlDataSource2" DataTextField="ENAME"
DataValueField="OUTLET_ID">


<asp:DropDownList ID="ddl_fsmid" runat="server" DataSourceID="SqlDataSource3"
DataTextField="FIRST_NAME" DataValueField="FSM_ID">

<asp:TextBox ID="Txtdate" runat="server" Height="20px"
Width="113px">
<asp:CalendarExtender ID="Txtdate_CalendarExtender" runat="server"
Enabled="True" PopupButtonID="TextBox33" TargetControlID="Txtdate">

<asp:TextBox ID="txtimei" runat="server" Height="18px" Width="117px">
<asp:TextBoxWatermarkExtender ID="txtimei_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtimei"
WatermarkText="IMEI ID">

<asp:Button ID="Button1" runat="server" onclick="insert_Click" Text="Insert" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BPConnectionString %>"
SelectCommand="SELECT [FIRST_NAME], [SFO_ID] FROM [SFO_INFO]">

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:BPConnectionString %>"
SelectCommand="SELECT [OUTLET_ID], [ENAME] FROM [OUTLET_INFO]">

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:BPConnectionString %>"
SelectCommand="SELECT [FSM_ID], [FIRST_NAME] FROM [FSM_INFO]">
ashu1810 13-Mar-12 5:55am    
this is grid view design code
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4" CssClass="style7"
ShowFooter="True"
style="font-family: Verdana; font-size: small; margin-bottom: 0px; margin-top: 0px;"
Width="111%" OnRowDataBound = "GridView1_RowDataBound" onselectedindexchanged="GridView1_SelectedIndexChanged"
Height="197px" >
<rowstyle backcolor="White" forecolor="#330099">
<columns>
<asp:TemplateField HeaderText="SFO ID">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server">

<footertemplate>



<itemtemplate>
<asp:Label ID="Label33" runat="server">


<asp:TemplateField HeaderText="Outlet Id">
<edititemtemplate>
<asp:TextBox ID="TextBox2" runat="server">

<footertemplate>



<itemtemplate>
<asp:Label ID="Label34" runat="server">


<asp:TemplateField HeaderText="FSM Id">
<edititemtemplate>
<asp:TextBox ID="TextBox4" runat="server">

<footertemplate>


<br />

<itemtemplate>
<asp:Label ID="Label36" runat="server">


<asp:TemplateField HeaderText="Current Date">
<edititemtemplate>
<asp:TextBox ID="TextBox3" runat="server">

<footertemplate>
<asp:TextBox ID="txtcurrdate" runat="server"
style="margin-bottom: 0px" ontextchanged="txtcurrdate_TextChanged">
<br />

<itemtemplate>
<asp:Label ID="Label35" runat="server" >


<asp:TemplateField HeaderText="Description" Visible="false">
<edititemtemplate>
<asp:TextBox ID="TextBox31" runat="server">

<footertemplate>
<asp:TextBox ID="Description" runat="server"
>
<br />

<%-- <itemtemplate>
<asp:Label ID="Label31" runat="server" Text='<%# bind("discount") %>'>


<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete">


--%>
<itemtemplate>
<asp:Label ID="Label31" runat="server">


<asp:TemplateField He

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900