Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Sat, 25-Oct-2014 Authors
Raja_89
20
Shoab.Shah
10
Jayakumars
10
All Time Authors
Jayakumars
4190
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)
 Forum Post
Hi,
I have a grid view which consists of 4 columns namely productID(AutoID), ProductName(dropdown),Price(textbox),Quantity(textbox),Total,Grant total(label)footer,Update or edit button,delete button.i have used in datatable in these parts like productname ,price,quantity in add button to display in gridview will be calculated total as well as grant total and suppose one more product i will added means calculation parts are clear so please find and i need delete the particular row ,edit the particular in datatable using without the access of database table .On command field of update btn the total will be calculated which is not working. And i need the grand total in the footer of my grid .
Im very new to C#.So i need the aspx coding and .cs coding help on this.
Waiting for the reply.
Regards,
Ramsakthi.R

Ramu.R

Posted by: Tapanparida36 on: 10/27/2012 [Member] Starter | Points: 25

Hi, you write this code in RowDataBound.......

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

decimal _Price= 0;
decimal _Quantity= 0;
decimal total= 0;


_Price= Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
_Quantity= Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Quantity"));
total= _Price * _Quantity;

Label ll=(Label)e.Row.Cells[4].FindControl("lbltotal");

ll.Text = total.ToString();


ViewState["total"] = (Convert.ToDecimal(ViewState["total"]) + ll).ToString();


}

// below code for calculate Grand total in footer template and display also.....
if (e.Row.RowType == DataControlRowType.Footer)
{


Label gnd_total = (Label)e.Row.Cells[3].FindControl("lbl_footerl");
gnd_total.Text = ViewState["total"].ToString();


}


}

Ramsakthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramsakthi on: 10/27/2012 [Member] Starter | Points: 25

Hi

its not working please try this and send it to me......

if (GridView1.Rows.Count == 0)
{

dt = new DataTable();

dt.Columns.Add("PID", typeof(int));
dt.Columns["PID"].AutoIncrement = true;
dt.Columns["PID"].AutoIncrementSeed = 1;
dt.Columns["PID"].AutoIncrementStep = 1;
dt.Columns.Add(new DataColumn("Item", typeof(System.String)));
dt.Columns.Add(new DataColumn("Price", typeof(System.String)));
dt.Columns.Add(new DataColumn("Quantity", typeof(System.String)));


}
else
dt = (DataTable)Session["DataTable"];

DataRow dr = dt.NewRow();
dr["Item"] = ddl_Product.SelectedItem.Text.Trim();
dr["Price"] = txt_Prodprice.Text.Trim();
dr["Quantity"] = txtQuantity.Text.Trim();

dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["DataTable"] = dt;

Ramu.R

Ramsakthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramsakthi on: 10/27/2012 [Member] Starter | Points: 25

<table width="100%" style="border:Solid 3px #D55500; width:100%; height:100%" cellpadding="0" cellspacing="0">
<tr style="background-color:#D55500">
<td colspan="4"
style=" height:10%; color:White; font-weight:bold; font-size:larger"
align="center">Product Details</td>
</tr>
<tr>
<td align="left">
:
</td>
<td>
&nbsp;</td>
<td align="right">&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td align="right">Product
</td>
<td>
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddl_Product" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl_Product_SelectedIndexChanged" Height="25px"
Width="155px">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="shampoo" Text="shampoo">shampoo</asp:ListItem>
<asp:ListItem Value="soap" Text="soap">soap</asp:ListItem>

<asp:ListItem Value="sippu" Text="sippu">sippu</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>

</td>
</tr>
<tr>
<td align="right">ProductDesc</td>
<td>
<asp:TextBox ID="txt_Desg" runat="server"/>
</td>
</tr>
<tr>

<td align="right">ProductPrice</td>
<td>
<asp:TextBox ID="txt_Prodprice" runat="server"/>
</td>
</tr>
<tr>
<td align="right">ProductQuality:
</td>
<td>
<asp:TextBox ID="txtQuantity" runat="server"/>
</td>
</tr>
<tr>
<td align="right">
&nbsp;</td>
<td align="center">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Button ID="btnUpdate" CssClass="classname" CommandName="Update" runat="server" Text="ADD" onclick="btnUpdate_Click" OnClientClick="return ProductValidations();"/>
</ContentTemplate>
</asp:UpdatePanel>

</td>
<td></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Label ID="lblNotification" runat="server" Visible="False" CssClass="Notifie"
ForeColor="#000099"></asp:Label>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td></td>
<td align="center" colspan="2">
&nbsp;</td></tr>
<tr>
<td align="center" colspan="2">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField
DataField="PID"
HeaderText="PID" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" ItemStyle-CssClass="price" />

<%-- <asp:TemplateField HeaderText = "Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text="0"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText = "Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Grand Total:
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
<%-- <asp:GridView runat="server" ID="gvdetails" DataKeyNames="ProductName"
AutoGenerateColumns="False" onrowcancelingedit="gvdetails_RowCancelingEdit"
onrowdeleting="gvdetails_RowDeleting" onrowediting="gvdetails_RowEditing" onrowupdating="gvdetails_RowUpdating"
>
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" /><Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" />
<asp:BoundField DataField="ProductQuality" HeaderText="ProductQuantity" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>--%>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>

Ramu.R

Ramsakthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramsakthi on: 10/27/2012 [Member] Starter | Points: 25

Hi tapanparida,

Please try this entire sorce code am used datatable in product details to display in gridview as well as to add multiple row .gridview using edit,delete in that particular row updation,deletions, total and footer granttotal please tried i need ...

Thanks & regards
Ramu R

Ramu.R

Ramsakthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tapanparida36 on: 10/29/2012 [Member] Starter | Points: 25

Hi , try this code

I have 3 fields in my table.(id,unit1,unit2) and display total unit in Gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound" ShowFooter="True" DataKeyNames="id">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:TemplateField HeaderText="unit1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("unit1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("unit1") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label4" runat="server" Text=""></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="unit2">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("unit2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("unit2") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label5" runat="server" Text=""></asp:Label>
</FooterTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


After that write this code in .cs page

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from calculate_table",con);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// create new column in a particular position
if (e.Row.RowType == DataControlRowType.Header)
{

TableHeaderCell NewCell = new TableHeaderCell();
NewCell.Text = "Grand Total";
e.Row.Cells.AddAt(3, NewCell); // here 3 means cell number


}

//end

if (e.Row.RowType == DataControlRowType.DataRow)
{
int x = 0;
int y = 0;
int z = 0;
x = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"unit1"));
y = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "unit2"));
z = x + y;

TableCell NewCell = new TableCell();
NewCell.ID = "NewCell";
NewCell.Text = z.ToString();
e.Row.Cells.AddAt(3, NewCell);

ViewState["tt1"] = (Convert.ToInt32(ViewState["tt1"])+x).ToString();
ViewState["tt2"] = (Convert.ToInt32(ViewState["tt2"]) + y).ToString();
ViewState["gt"] = (Convert.ToInt32(ViewState["gt"]) + z).ToString();



}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label l1 = (Label)e.Row.Cells[1].FindControl("Label4");
l1.Text = ViewState["tt1"].ToString();

Label l2 = (Label)e.Row.Cells[2].FindControl("Label5");
l2.Text = ViewState["tt2"].ToString();

TableCell NewCell = new TableCell();
NewCell.ID = "NewCell";
NewCell.Text = ViewState["gt"].ToString();
e.Row.Cells.AddAt(3, NewCell);

}

}

I think this code will help you .

Thanks
T.Kumar

Ramsakthi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response


Found interesting? Add this to: