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

    Double the textbox values in gridview (datatable) if same row from datlist is clicked

    i am having a datalist and a gridview ..
    i am having a button inside an itemtemplate in datalist.
    i am handling an item command event
    when i click on a button a row is added to datatable in gridview ..
    if same row again is clicked ...i want a column value (quantity) to double in it..

    What I have tried:

    source view :

    <asp:GridView ID="BillingGridView" runat="server" AutoGenerateColumns="false"
    AllowSorting="True" OnPageIndexChanging="BillingGridView_PageIndexChanging"
    OnRowDataBound="BillingGridView_RowDataBound"
    OnSorting="BillingGridView_Sorting1" OnRowCreated="BillingGridView_RowCreated1"
    OnRowCommand="BillingGridView_RowCommand" DataKeyNames="ItemId"
    TabIndex="-1" Width="100%"
    PageSize="7" BackColor="White" BorderColor="#336666" BorderStyle="Double"
    BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
    style="margin-bottom: 0px">
    <columns>
    <asp:TemplateField HeaderText="SNo">
    <itemtemplate>
    <%# Container.DataItemIndex + 1 %>



    <asp:BoundField HeaderText="ItemId" DataField="ItemId" />

    <asp:BoundField HeaderText="Item" DataField="ItemName" />



    <asp:TemplateField HeaderText="Quantity">

    <itemtemplate>

    <asp:TextBox ID="TxtQty" runat="server" Text='<%#Eval("Quantity") %>' OnTextChanged="TxtQty_TextChanged"
    AutoPostBack="true" onkeypress="return validatenumerics(event);">




    <asp:BoundField HeaderText="Units" DataField="Units" />
    <asp:BoundField HeaderText="Price" DataField="UnitPrice" />


    <asp:TemplateField HeaderText="Disc %">

    <itemtemplate>

    <asp:TextBox ID="TxtDisc" runat="server" Text='<%#Eval("Discount") %>' OnTextChanged="TxtDisc_TextChanged"
    AutoPostBack="true" onkeypress="return validatenumerics(event);" >




    <asp:BoundField HeaderText="Value" DataField="Value" />
    <asp:TemplateField HeaderText="Delete Item">
    <itemtemplate>
    <asp:Button ID="lnkdelete" runat="server" Text="Del" CausesValidation="false" OnClick="lnkdelete_Click" OnClientClick="return ConfirmMethod('DeleteItem')" />




    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="White" Height="30px" Font-Size="10" Font-Names="Calibri"
    HorizontalAlign="Center" ForeColor="#333333" />
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle Font-Size="12" Font-Names="Calibri" BackColor="#336666" Font-Bold="True"
    ForeColor="White" />
    <EditRowStyle BorderColor="Silver" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#487575" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#275353" />


    <asp:DataList runat="server" ID="datalist" RepeatColumns="1" RepeatDirection="Horizontal"
    OnItemDataBound="datalist_ItemDataBound"
    OnItemCreated="datalist_ItemCreated" AlternatingItemStyle-Wrap="true" CellSpacing="10"
    OnItemCommand="datalist_ItemCommand" CssClass="Itemdatalist_div"
    Height="219px" Width="477px" DataKeyNames="ItemId" EmptyDataText="No Rows To Display">
    <AlternatingItemStyle Wrap="True" />
    <itemtemplate>

    <%--

    <asp:Label ID="PriceLabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "UnitPrice")%>'>
    --%>
    <asp:Button ID="testing" ForeColor="#353432" Font-Bold="true" Font-Size="Smaller" CausesValidation="true" BorderStyle="Dotted" BackColor="#EBFAE6" runat="server" BorderColor="Black"
    OnClientClick='<%#String.Format("return SetItemDetails({0},{1},{2},{3},{4})", Eval("ItemId"), Eval("ItemName"),Eval("Cost"),Eval("Dateofexpire"),Eval("Quantity"), Eval("Units"),Eval("UnitPrice"),Eval("Location"),Eval("Description")) %>'
    OnClick="testing_Click" Height="35px" Width="100%" Text='<%#"("+ DataBinder.Eval(Container.DataItem, "ItemName")+")"+" (Price:"+ DataBinder.Eval(Container.DataItem, "UnitPrice")+")"+" (Qty:"+ DataBinder.Eval(Container.DataItem, "Quantity")+")"+" (units:"+ DataBinder.Eval(Container.DataItem,"Units")+")"+" (Location:" + DataBinder.Eval(Container.DataItem,"Location")%>'
    CommandArgument='<%#Eval("ItemId") %>' ToolTip ='<%#"Qty:" +"("+ DataBinder.Eval(Container.DataItem, "Quantity")+")"+ " Location:" + DataBinder.Eval(Container.DataItem,"location")+ " Units:" + DataBinder.Eval(Container.DataItem,"Units")%>'/>







    code view :

    protected void datalist_ItemCommand(object source, DataListCommandEventArgs e)
    {

    if (Session["mytable"] == null)
    {
    dt = new DataTable("mytable");
    dt.Columns.Add("ItemId", typeof(string));
    dt.Columns.Add("ItemName", typeof(string));
    dt.Columns.Add("Cost", typeof(string));
    dt.Columns.Add("Dateofexpire", typeof(string));
    dt.Columns.Add("Quantity", typeof(string));
    dt.Columns.Add("Units", typeof(string));
    dt.Columns.Add("UnitPrice", typeof(string));
    dt.Columns.Add("Location", typeof(string));
    dt.Columns.Add("Description", typeof(string));
    dt.Columns.Add("Discount", typeof(string));
    dt.Columns.Add("Value", typeof(string));
    }
    else
    {
    //If yes then get it from current session
    dt = (DataTable)Session["MyTable"];
    }
    int empid = Convert.ToInt32(e.CommandArgument.ToString());
    strSqlCommmand = "Select * from ItemsMaster Where ItemId=@ItemId";
    cn = new SqlConnection("Data Source=USER;User Id=sa;Password=123456;Database=BillData");
    if (cn.State != ConnectionState.Open)
    cn.Open();
    cmd = new SqlCommand(strSqlCommmand, cn);
    cmd.Parameters.AddWithValue("ItemId", empid);
    SqlDataReader dr = cmd.ExecuteReader();
    try {
    if (dr.Read())
    {

    int itemid= Convert.ToInt32(dr["ItemId"].ToString());
    string itemname= dr["ItemName"].ToString();
    string cost= dr["Cost"].ToString();
    string datime= dr["Dateofexpire"].ToString();
    string qlty = dr["Quantity"].ToString();
    string units = dr["Units"].ToString();
    string unitprice= dr["UnitPrice"].ToString();
    string loctn = dr["Location"].ToString();
    string desc = dr["Description"].ToString();
    // txtCustName.Text = dr["ItemId"].ToString();
    DataRow nw = dt.NewRow();
    nw["ItemId"] = itemid.ToString();
    nw["ItemName"] = itemname.ToString();
    nw["Cost"] = cost.ToString();
    nw["Dateofexpire"] = datime.ToString();
    nw["Quantity"] = qlty.ToString();
    nw["Units"] = units.ToString();
    nw["UnitPrice"] = unitprice.ToString();
    nw["Location"] = loctn.ToString();
    nw["Description"] = desc.ToString();
    nw["Discount"] = "";
    nw["Value"] = "";
    dt.Rows.Add(nw);
    Session["mytable"] = dt;
    BillingGridView.DataSource = dt;
    BillingGridView.DataBind();
    dr.Close();
    cn.Close();
    }

    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }

    }
  • #768894
    one more requirement i want for the code is , when i click on the button (inside datalist) for multiple times, the datatable should be checked whether the row is already added to the datatable .If already present , i want to update column (quantity) . help me out!!!!


  • Sign In to post your comments