Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I have a panel where there are three texboxes and two dropdowns. When I click on Button in that panel, it should add the data into a Grid below.
txt1
txt2
txt3
ddl1
ddl2 Button

On Button Click... add the fields into a gridview below and clear texboxes in panel and allow user to add more line items.

1st row: txt1 txt2 txt3 ddl1 ddl2
2nd row : continues

How to achieve this? Your help is much appreciated.
Posted
Updated 7-Feb-21 17:05pm
Comments
Richard MacCutchan 7-Jan-15 6:47am
   
What have you tried? Have you checked the documentation to see what method you should call to add a new row?
sudevsu 7-Jan-15 8:34am
   
I tried creating a DataTable with columns and add rows on button click to that Datatable and Databind (bind Datatable to Grid) But It only does one row. I want to continue appending.

Below given is an example to achive the same.
Code for .aspx Page
//---------------------
<asp:Button runat="server" ID="btnAddRows" Text="Add New Row"
        onclick="btnAddRows_Click"  />
    <asp:GridView runat="server" ID="grd1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="COL1" HeaderText="col1" />
        <asp:BoundField DataField="COL2" HeaderText="col2"/>
        <asp:BoundField DataField="COL3" HeaderText="col3"/>
    </Columns>
    </asp:GridView>

//---------------------
Code for .cs Page

// -- Property to assign as datasource
public DataTable GridData
   {
       get {
           if(ViewState["GridData"]==null)
           {
               DataTable dt = new DataTable("GridData");
               dt.Columns.Add(new DataColumn("COL1"));
               dt.Columns.Add(new DataColumn("COL2"));
               dt.Columns.Add(new DataColumn("COL3"));
               dt.Columns.Add(new DataColumn("COL4"));
               ViewState["GridData"] = dt;
               return dt;
           }
           else
               return (DataTable)ViewState["GridData"];

       }
       set { ViewState["GridData"] = value; }
   }

   // -- Add button click event
   protected void btnAddRows_Click(object sender, EventArgs e)
   {
       DataRow Dr = GridData.NewRow();
       Dr["COL1"] = "txt1"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
       Dr["COL2"] = "txt2"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
       Dr["COL3"] = "ddl1"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
       Dr["COL4"] = "ddl2"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
       GridData.Rows.Add(Dr);
       BindGrid();

       // ---- CLEAR TEXT BOX & DDL BELOW

   }
   // -- Grid bind method need to call on each action ( add / remove)
   protected void BindGrid()
   {
       grd1.DataSource = GridData;
       grd1.DataBind();
   }
   // -- first time Grid bind on page load
   protected void Page_Load(object sender, EventArgs e)
   {
       if(!IsPostBack)
       {
           BindGrid();
       }
   }
   
Comments
sudevsu 7-Jan-15 8:41am
   
Thank you Pankaj, I have problem here. I tried the following way. Please check if this code is proper.


If Not Page.IsPostBack Then
CreateDataTable()
End If

Protected Sub CreateDataTable()

dt.Columns.Add(New DataColumn("ItemNo", GetType(String)))
dt.Columns.Add(New DataColumn("DOT", GetType(String)))
dt.Columns.Add(New DataColumn("32nds", GetType(String)))
dt.Columns.Add(New DataColumn("Removal Area", GetType(String)))
dt.Columns.Add(New DataColumn("Position", GetType(String)))
dt.Columns.Add(New DataColumn("Tire Size", GetType(String)))
dt.Columns.Add(New DataColumn("Product code", GetType(String)))
dt.Columns.Add(New DataColumn("Approval Number", GetType(String)))
dt.Columns.Add(New DataColumn("Reason for Removal", GetType(String)))
dt.Columns.Add(New DataColumn("File", GetType(String)))
End Sub

Protected Sub btnAddLineItems_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddLineItems.Click
Try
CreateDataTable()
Dim dr As DataRow = dt.NewRow()
dr("ItemNo") = 1
dr("DOT") = txtDOT.Text.ToString().Trim()
dr("32nds") = txtTDepth.Text.ToString().Trim()
If ddlRArea.SelectedIndex > 0 Then
dr("Removal Area") = ddlRArea.SelectedValue.ToString().Trim()
End If
If chkTirePos.SelectedIndex > 0 Then
dr("Position") = chkTirePos.SelectedValue.ToString().Trim()
End If
dr("Tire Size") = txtTireSize.Text.ToString().Trim()
dr("Product code") = txtProdcode.Text.ToString().Trim()
dr("Approval Number") = txtApprovalno.Text.ToString().Trim()
dr("Reason for Removal") = txtRR.Text.ToString().Trim()
Dim filename As String = fileup.PostedFile.FileName
If (fileup.HasFile) Then
fileup.SaveAs(Server.MapPath("~/UploadedFiles/" + fileup.FileName))
dr("File") = fileup.FileName.ToString().Trim()

Else
dr("File") = ""
End If
dt.Rows.Add(dr)
gdvLineItems.DataSource = dt
gdvLineItems.DataBind()
txtDOT.Text = String.Empty
txtTDepth.Text = String.Empty
ddlRArea.SelectedIndex() = -1
chkTirePos.SelectedIndex() = -1
txtTireSize.Text = String.Empty
txtProdcode.Text = String.Empty
txtApprovalno.Text = String.Empty
txtRR.Text = String.Empty
Catch ex As Exception

End Try




End Sub




<asp:UpdatePanel ID="updgrid" runat="server" >
<contenttemplate>
<table width="100%" cellspacing="3" cellpadding="3">
<tr class="RowOdd">
<td width="7.5%" class="Labels">DOT Number</td>
<td width="16.5%"><asp:TextBox ID="txtDOT" runat="server" Width="100%" /></td>
<td width="1%"><asp:RequiredFieldValidator ID="Ref25" runat="server" ControlToValidate="txtDOT" ErrorMessage="*" ForeColor="Red" /></td>
<td width="7.5%" class="Labels">Tire Depth 32nds</td>
<td width="16.5%"><asp:TextBox ID="txtTDepth" runat="server" Width="100%" /></td>
<td width="1%"><asp:RequiredFieldValidator ID="Ref26" runat="server" ControlToValidate="txtTDepth" ErrorMessage="*" ForeColor="Red" /></td>
<td width="7.5%" class="Labels">Removal Areas</td>
<td width="16.5%">
<asp:DropDownList ID="ddlRArea" runat="server" AutoPostBack="true" Font-Size="Small" ForeColor="Black">
<asp:ListItem>Bead
<asp:ListItem>Tread
<asp:ListItem>Sidewalk
<asp:ListItem>Other
<asp:ListItem>
sudevsu 7-Jan-15 8:44am
   
So My question in here is in btnAddLineItems_Click,

1)I called CreateDataTable() because if I don't call, it shows Datatable as empty. Even after creating datatable columns. So I called that method in AddLineItems.
2) Autoeventwireup= false. what is correct it should true / false?
   
Comments
sudevsu 7-Jan-15 8:56am
   
Thank you Mohamed. But this is not what I am looking for. however this may help me in near future.
   
Comments
Deepu S Nair 7-Jan-15 8:07am
   
same solution twice.why?
MohamedEliyas 7-Jan-15 8:09am
   
1st solution only using textbox and then 2nd using for dropdownlist so i give you
Deepu S Nair 8-Jan-15 2:17am
   
Then please use Improve Solution Link.Otherwise it may cause downvoting your solution.
Thanks to every one in here who helped in achieving this. And special thanks
Kumar Pankaj Verma
for making me understand it clear.
I added
XML
<asp:UpdatePanel ID="Upnl1" runat="server">
<Triggers>
 <asp:PostBackTrigger ControlID="btnAddLineItems" />
</Triggers>
 <ContentTemplate>
    <div>
    </div>
    <asp:panel>
    <table>
        txt1
        txt2
        ddl1
        ddl2
        File upload
        Button Add new Items
    </table>
    </asp:panel>

    <asp:updatepanel>
        <ContentTemplate>
        <Gridview>
        <Boundfield 1=txt1>
        <Boundfield 2=txt2>
        <Boundfield 3=ddl1>
        <Boundfield 4=ddl3>
        <Templatefield Link button="Download">
        </Gridview>
        </ContentTemplate>
    <asp:updatepanel>

 </ContentTemplate>
</asp:UpdatePanel>


And on clicking Linkbutton in Gridview, download the file that is attached from File upload and Button Add new Items.
   
How to change to control value for the data row part?

// -- Property to assign as datasource
public DataTable GridData
{
get {
if(ViewState["GridData"]==null)
{
DataTable dt = new DataTable("GridData");
dt.Columns.Add(new DataColumn("COL1"));
dt.Columns.Add(new DataColumn("COL2"));
dt.Columns.Add(new DataColumn("COL3"));
dt.Columns.Add(new DataColumn("COL4"));
ViewState["GridData"] = dt;
return dt;
}
else
return (DataTable)ViewState["GridData"];

}
set { ViewState["GridData"] = value; }
}

// -- Add button click event
protected void btnAddRows_Click(object sender, EventArgs e)
{
DataRow Dr = GridData.NewRow();
Dr["COL1"] = "txt1"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
Dr["COL2"] = "txt2"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
Dr["COL3"] = "ddl1"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
Dr["COL4"] = "ddl2"; // I USED FIXED DATA HERE BUT U CAN USE YOUR CONTROL VALUE
GridData.Rows.Add(Dr);
BindGrid();

// ---- CLEAR TEXT BOX & DDL BELOW

}
// -- Grid bind method need to call on each action ( add / remove)
protected void BindGrid()
{
grd1.DataSource = GridData;
grd1.DataBind();
}
// -- first time Grid bind on page load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
   
Comments
CHill60 8-Feb-21 4:24am
   
This is not a solution to the original question.
If you have a question of your own, then use the red "Ask a Question" link at the top of this page. You will need to provide a much clearer explanation of your problem then you have provided here
Member 15067202 11-Feb-21 0:22am
   
It is fine. I managed to solve the problem

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