.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to remove a row in gridview based on session variable

Posted By: srinivas     Posted Date: February 18, 2012    Points:5   Category :ASP.Net
hello sir,i binded a page gridview(gridproducts) session values to new page gridview(gridcart).(plz evaluate the below aspx code for your clarity).for suppose 2 items are added to the gridcart(gridview) from prev gridview(not mentioned in below code).i added a button next to it(remove).now my intention is to remove that particular item from the gridview.and it should display only 1 item.and i'm not altering any data values in table with sql commands.all the data stored in session temporarily.now i'm trying to make changes for it.and also facing problems with session null value exception how can i overcome this.and 1 more question is .. how to display gridview with empty table.
thanks in adv.

<asp:GridView ID="gridcart" runat="server" AllowPaging="true"
AutoGenerateColumns="False" Width="754px" Height="265px"
BackColor="#FFCC99" BorderColor="#CC0000" BorderStyle="Groove"
BorderWidth="10px" CellPadding="0" Font-Names="Times New Roman"
ForeColor="Black" HorizontalAlign="Left">
<asp:TemplateField HeaderText="Model No" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate><asp:Label ID="lbl123" runat="server" Text='<%#Eval("MODELNO")%>'/></ItemTemplate>

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<asp:TemplateField HeaderText="Company" ItemStyle-HorizontalAlign="Center" >

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate><asp:Label ID="lblprice" runat="server" Text='<%#Eval("PRICE")%>'/></ItemTemplate>

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<asp:TemplateField HeaderText="Stock" ItemStyle-HorizontalAlign="Center" >

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate><asp:TextBox ID="textquan" Height="20px" Width="40px" MaxLength="3" runat="server" AutoPostBack="false" OnTextChanged="text"/></ItemTemplate>

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<asp:TemplateField HeaderText="TotalPrice" ItemStyle-HorizontalAlign="Center" >
<asp:Label ID="lbltp" runat="server"/>

<HeaderStyle Height="5px"></HeaderStyle>
<ItemStyle Height="2px"/>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Button ID="btnrem" runat="server" Text="Remove" OnClick="click"/></ItemTemplate>
<HeaderStyle BackColor="Red" BorderStyle="None" ForeColor="#FFFF66"
HorizontalAlign="Center" />
<RowStyle BackColor="#CCFFCC" BorderStyle="None" ForeColor="Black"
HorizontalAlign="Center" />

Author: Mahesh             
Posted Date: March 01, 2012     Points: 20   

As per me i think you should create a datatable having structure as that of grid columns also add a column "IsActive" to it as,
This "IsActive" column will have 'Y' & 'N' values,

Dim oDataTable as New DataTable

then assigned data to that table by having db trip initially and set all rows "IsActive" column to 'Y'

bind the datatable to gridcart as,
gridcart.datasource = odatatable.select("IsActive = 'Y'")

now whenever you click on the button you set the grid column "IsActve" to 'N'
and again bind the grid as,
gridcart.datasource = odatatable.select("IsActive = 'Y'")

Hope this helps you....


Author: Pawan Awasthi             
Posted Date: March 24, 2012     Points: 20   

Hai Srinivas,
For your fist question where you want to remove the record from the GridView as well as from the session, and you are getting the empty for the session value-
There is no need to keep the records in Session variable as the session variable are heavy weight and it increases the load on the page. So please try to avoid sing session at least in this scenario.
You can use the view state instead.
As your remove button at the last column of the GridView. You need to first find the button using FindControl() method and then you can keep the data in to the ViewState.
After click on the remove button, delete the row from the gridview and again load the grid with keeping the data in to View State.
You can see the below code snippet for your reference:

protected void gvEmployeeId_RowDeleting(Object sender, GridViewDeleteEventArgs e)
dtQuery = (DataTable)ViewState["AllRecords"];
if (dtQuery != null) dtQuery.Rows.RemoveAt(e.RowIndex);

For your questions "how to display gridview with empty table.", you need to create a table with the required columns and then load the Gridview with the table. it will show the empty records.
See the below code for reference:

private void BindEmptyEmployeeGrid()
dtQuery = new DataTable();
dtQuery.Columns.Add("Employee Id");
dtQuery.Rows.Add(new object[] { "" });
gvEmployeeId.DataSource = dtQuery;
ViewState["AllRecords"] = dtQuery;

Hope it will help you.

Pawan Awasthi
(Whats App)+91 8123489140
(cell No.) +6 0143651476

Skype: Pawansoftit
Author: srinivas             
Posted Date: March 26, 2012     Points: 20   

thanks for all your efforts.

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend