Click here to Skip to main content
15,310,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my shopping cart project I have a datalist and a gridview. I have 2 products in database and is displayed through datalist on pageload. I want to add these products in gridview one by one on
selection using 'add to cart' button. I am able to add blank rows in gridview on click of that 'add to cart' button but unable to assign values to respective textbox's. Can any one help me out?
Hereunder is my code:

XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<table width="100%">
<asp:DataList ID="Datalist1" runat="server" DataKeyField="item_code"  RepeatDirection="Horizontal"
RepeatLayout="Table"  OnItemCommand="dl_item_command"  >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
Item code:
<asp:Label Id="Itmcodelbl" runat="server" Text='<%#Eval("item_code") %>'>
</asp:Label>
<br />
Item Name:
<asp:Label Id="Itmnamelbl" runat="server" Text='<%#Eval("item_name") %>'>
</asp:Label>
<br />
Particulars:
<br />
<asp:TextBox Id="TB1" runat="server" Text='<%#Eval("item_desc") %>'
Height="94"   ReadOnly="true"  TextMode="MultiLine"  BorderStyle="None">
</asp:TextBox>
<br />
<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("Photopath","~/photo/{0}") %>' style="padding-left:40px; border-color:Green; border-left-style:groove; border-width:thin"/><br />
Buy :
<asp:TextBox Id="TB2" runat="server" Text="" Width="24" OnTextChanged="TB2_TextChanged" MaxLength="6" AutoPostBack="true"
Height="12"     BorderStyle="Groove">
</asp:TextBox>
<asp:Button ID="Button18" runat="server" ForeColor="White"   commandname="myevent"
BackColor="Teal" Font-Size="small"
style="z-index: 1;font-family:Sans-Serif; height:22px;  font-weight:900; "
Text="Add to cart" />
 </ItemTemplate>
</asp:DataList>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label id="Label4" runat="server"/>



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
v_rowindextag = "";
rowIndex = 0;
SetInitialRow();
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
con.Open();
string cmdstr = "Select * from item";
SqlCommand cmd = new SqlCommand(cmdstr, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
Datalist1.DataSource = ds.Tables[0];
Datalist1.DataBind();
}

}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView3.DataSource = dt;
GridView3.DataBind();
}

public void dl_item_command(Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "myevent") // check commandname here
{
int index = e.Item.ItemIndex;
Label lbl = (Label)Datalist1.Items[index].FindControl("Itmnamelbl");
Label4.Text = lbl.Text;
Label lbl1 = (Label)Datalist1.Items[index].FindControl("Itmcodelbl");
int v_code = Convert.ToInt32(lbl1.Text);
AddNewRowToGrid();
}

}
private void AddNewRowToGrid()
{
rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//adding new row to grid
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
//extract the TextBox values
TextBox box1 = (TextBox)GridView3.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView3.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView3.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView3.Rows[rowIndex].Cells[4].FindControl("TextBox4");
//box1.Text = Label4.Text;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
rowIndex++;
v_rowindextag = "*";
}

}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView3.DataSource = dtCurrentTable;
GridView3.DataBind();
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}

private void SetPreviousData()
{
rowIndex =0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i =v_int; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)GridView3.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView3.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView3.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView3.Rows[rowIndex].Cells[4].FindControl("TextBox4");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
rowIndex++;
}
}
}
Posted

1 solution

Hi S.R.

here i am sending my logic, hope it'll help you

C#
//vitual table
   public static DataTable Table()
   {
       DataTable dtGrd = new DataTable();
       dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
       dt.Columns.Add(new DataColumn("Column1", typeof(string)));
       dt.Columns.Add(new DataColumn("Column2", typeof(string)));
       dt.Columns.Add(new DataColumn("Column3", typeof(string)));
       dt.Columns.Add(new DataColumn("Column4", typeof(string)));
       return dtGrd;
   }


public void dl_item_command(Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "myevent") // check commandname here
{
int index = e.Item.ItemIndex;
Label lbl = (Label)Datalist1.Items[index].FindControl("Itmnamelbl");
Label4.Text = lbl.Text;
Label lbl1 = (Label)Datalist1.Items[index].FindControl("Itmcodelbl");
TextBox TB1 = (TextBox)Datalist1.Items[index].FindControl("TB1");
TextBox TB2 = (TextBox)Datalist1.Items[index].FindControl("TB2");
int v_code = Convert.ToInt32(lbl1.Text);
DataRow dr = dtdata.NewRow();
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = lbl.Text;
dr["Column2"] = lbl1.Text;
dr["Column3"] = TB1.Text;
dr["Column4"] = TB2.Text;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView3.DataSource = dt;
GridView3.DataBind();
//AddNewRowToGrid();
}
}
   

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