Column sum of only visible row - c#

I am trying to calculate the sum of the grand total column and showing it to the footer of the gridview, which I am able to achieve it. However I have a condition which makes some rows invisible from the gridview, but it is also summing the value of the rows which are invisible. How to sum the column value only for the rows which are visible?
Below is my gridview:
<asp:GridView ID="GridView9" runat="server" AllowPaging="false" AutoGenerateColumns="False"
ShowFooter="true" BackColor="White" DataKeyNames="ID" BorderColor="#999999" CssClass="tableUserInfo"
GridLines="Vertical" ShowHeaderWhenEmpty="True" PageSize="10" OnRowDataBound="GridView9_RowDataBound">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="SessionId" Visible="true">
<ItemTemplate>
<asp:TextBox ID="txt_GSessionId" runat="server" Text='<%# Eval("SessionId") %>' ReadOnly="true"
Visible="false"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="10%" />
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer" Visible="true">
<ItemTemplate>
<asp:Label ID="lblCustomerName" Font-Size="11px" runat="server" Text='<%# Eval("CustomerName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="8%" />
<ItemStyle Width="8%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="G_Total" Visible="true">
<ItemTemplate>
<asp:Label ID="lblGTotal" Font-Size="11px" runat="server" Text='<%# Eval("GTotal","{0:n}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
<FooterStyle Width="30%" />
<HeaderStyle Width="8%" />
<ItemStyle Width="8%" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="Black" ForeColor="Red" Width="100%" Wrap="false" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Here is my code behind:
decimal grdTotal;
protected void GridView9_RowDataBound(object sender, GridViewRowEventArgs e)
{
txt_SessionId.Text = ddlSession.SelectedValue;
foreach (GridViewRow row2 in GridView9.Rows)
{
TextBox sid = row2.FindControl("txt_GSessionId") as TextBox;
int a, b;
a = int.Parse(sid.Text);
b = int.Parse(txt_SessionId.Text);
if (a == b)
{
row2.Visible = true;
}
else
{
row2.Visible = false;
}
}
if (e.Row.Visible && e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "GTotal"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
Label lbl1 = (Label)e.Row.FindControl("lblLabourTotal");
lbl.Font.Size = 12;
lbl.Text = "Rs. " + grdTotal.ToString("0");
lbl.ForeColor = System.Drawing.Color.White;
}
}

Related

asp net c# get value of specific row in gridview using checkbox

I have a gridview with three columns admin id, admin name and checkbox, and there is a single button below the gridview,
I want to get the value of admin name column and display it in a label when I check the checkbox and then click the button,
I tried doing this and its working fine but I can get one value only for example if I checked two checkbox and then clicked the button it get only the last value, and I want to get them all.
<asp:GridView ID="GridView1" CssClass="datatable" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="10"
AllowSorting="True" EnableViewState="false" width="863px" DataSourceID="SqlDataSource1" >
<Columns >
<asp:TemplateField HeaderText="req_id" >
<ItemTemplate >
<asp:Label ID="label_id" runat="server" Text='<%# Eval("admin_id") %>' ></asp:Label>
</ItemTemplate>
<ItemStyle Width="20px" />
<HeaderStyle Width="30px" />
<FooterStyle Width="20px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="admin_number">
<ItemTemplate >
<asp:Label ID="label_name" runat="server" Text='<%# Eval("admin_name") %>' ></asp:Label>
</ItemTemplate>
<ItemStyle Width="20px" />
<HeaderStyle Width="60px" />
<FooterStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="privileges" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox_privileges" runat="server" Checked='<%# Convert.ToInt32(Eval("prev_1")) == 1 ? true : false %>' />
</ItemTemplate>
<ItemStyle Width="90px" />
<HeaderStyle Width="90px" />
<FooterStyle Width="90px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void btn_save(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox_privileges") as CheckBox);
if (chkRow.Checked)
{
string name= (row.Cells[1].FindControl("label_name") as Label).Text;
Label4.Text = name;
}
}
}
}
In this line you are overriding the text:
Label4.Text = name;
To add all the text you should do something like this:
Label4.Text = name + Label4.Text;

How to populate and display a child gridview in asp.net

I am working on an asp.net C# project. I have created a nested gridview (gv). The primary key for the child gv data is the DatakeyNames value of the parent gv. The DatakeyNames is set to SchemeId. The problem I am having is that if I populate the child gv from code-behind using the DatakeyNames's SchemeId then the child gv is not displayed. I tried to get the value using a hiddenfield withing a ItemTemplate. The problem with this is that an empty column is displayed at the end of the grid view. To avoid this, I tried to add the hiddenfield as part of another ItemTemplate, then also the child gv is not displayed. Then I tried visible=False on the ItemTemplate containing the hiddenfield, but still an empty column was displayed. This is very strange. Any help is greatly appreciated.
<asp:GridView ID="grdParent" BackColor="#f1f1f1" CellPadding="0" CellSpacing="0"
AutoGenerateColumns=false DataKeyNames="SchemeId"
runat="server" OnRowDataBound="grdParent_RowDataBound" Width="80%" OnRowCreated="grdParent_OnRowCreated" >
<AlternatingRowStyle BackColor="White" />
<RowStyle Font-Size="12px" VerticalAlign="Top" Height="30px" />
<HeaderStyle BackColor="#57668A" ForeColor="#d6d6d6" Font-Bold="True" Font-Size="14px" Height="35px"/>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("PlanedScheduleId") %>', 'one');">
<img id="imgdiv<%# Eval("PlanedScheduleId") %>" alt="Click to show/hide Orders for Customer <%# Eval("PlanedScheduleId") %>" width="10px" height="10px" src="images/Minus-26.png"/>
</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<ItemTemplate>
<asp:Label ID="lblDate" Text='<%# Eval("Date") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate >
<asp:HiddenField ID="hfSchemeId" runat="server" Value='<%# Eval("SchemeId") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div class="divBackground" id="div<%# Eval("PlanedScheduleId") %>" style="position:relative;OVERFLOW: auto; WIDTH: 97%;padding:0px 20px 20px 31px" >
<asp:GridView ID="grdChild" BackColor="White" Width=100% Font-Size=X-Small
AutoGenerateColumns=false Font-Names="Verdana" runat="server" DataKeyNames="SchemeId"
BorderStyle=Double BorderColor="#57668A">
<RowStyle Font-Size="12px" VerticalAlign="Top" height="25px" />
<HeaderStyle BackColor="#B0C4DE" ForeColor="Black" Font-Bold="True" Font-Size="12px" Height="30px"/>
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Start Date" >
<ItemTemplate>
<asp:Label ID="lblOrderID" Text='<%# Eval("StartDate") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Time">
<ItemTemplate><%# Eval("StartTime")%></ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date" >
<ItemTemplate>
<asp:Label ID="Label2" Text='<%# Eval("EndDate") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="End Time">
<ItemTemplate><%# Eval("EndTime")%></ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void grdParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (e.Row.RowType == DataControlRowType.DataRow)
{
int SchemeId = Convert.ToInt32(grdParent.DataKeys[e.Row.RowIndex].Values[0]);
GridView gv = new GridView();
gv = (GridView)row.FindControl("grdChild");
gv.DataSource = ChildDataSource(SchemeId.ToString());
gv.DataBind();
}
}

Edit Command in Gridview

I am extracting data from database in gridview, where I have given a link button "Edit" for editing the data of that row. However while editing , one of the columns(YEAR) of my gridview becomes a dropdown list. While fetching the data from database, the Column "Year" consists of label and in edit mode it gets changed to dropdownlist. Moreover I added a condition to make the link buttons appear/disappear based on the value of the Label "Year". When I edit the particular row it throws an error because as soon as edit command is called it converts the column into dropdownlist however the row_databound searches for label. It shows "Object not set to an instance....." PLease help
Aspx
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource2" OnRowDataBound="gv1_RowDataBound" OnRowUpdating="gv1_RowUpdating" DataKeyNames="tid" CssClass="auto-style1" Width="694px">
<Columns>
<asp:TemplateField HeaderText="Sr No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="tid" SortExpression="tid" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("tid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<EditItemTemplate>
<asp:TextBox ID="txtTitle1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="category">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Bind("category") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="rbtnlist" runat="server"><asp:ListItem Text="Soft Skills"></asp:ListItem><asp:ListItem Text="Technical Skills"></asp:ListItem></asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Announced at" SortExpression="dt">
<ItemTemplate>
<asp:Label ID="lblDt" runat="server" Text='<%# Bind("dt", "{0:dd/MMM/yyyy HH:mm tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year" SortExpression="year">
<EditItemTemplate>
<asp:DropDownList ID="ddlyr" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblyr" runat="server" Text='<%# Bind("year") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Id" SortExpression="userid">
<ItemTemplate>
<asp:Label ID="lblUid" runat="server" Text='<%# Bind("userid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btndel" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return isConfirm()" Visible="false"></asp:LinkButton>
<%--<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="btnupdte" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncncl" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
c#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbly = (Label)e.Row.FindControl("lblyr");
if (lbly.Text == DateTime.Now.Year.ToString() || lbly.Text == ((DateTime.Now.Year)-1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year)-1) ; i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}
}
I am assuming that you are binding your grid with a DataTable with colum name "Year". Try below code where dt refers to your Datatable:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowno = e.Row.DataItemIndex;
//Suppose dt is DataTable to which you are binding your grid
//and year is the column name within this datatable
string year = Convert.ToString(dt.Rows[e.Row.DataItemIndex]["year"]);
if (year == DateTime.Now.Year.ToString() || year == ((DateTime.Now.Year) - 1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year) - 1); i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}

How to find TextBox in GridViewRow edit mode

I have tried several so called answers for this and it has me lost. I am simply trying to default a TextBox Text value with today's date and time, but I cannot find the control when I click LinkButton with CommandName "Edit".
Here is my gridview...
<asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3"
DataSourceID="sdsSignInRegister" ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" GridLines="Vertical" OnRowCommand="gvSignInRegister_RowCommand1">
<Columns>
<asp:TemplateField HeaderText="Returned" SortExpression="DateTimeReturned">
<EditItemTemplate>
<asp:TextBox ID="txtReturned" runat="server"></asp:TextBox>
<asp:ImageButton runat="Server" ID="calImg" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
<asp:RequiredFieldValidator ID="rfv1" runat="server" SetFocusOnError="true" ValidationGroup="vg1" ControlToValidate="txtReturned" ErrorMessage="Required"></asp:RequiredFieldValidator>
<ajaxToolkit:CalendarExtender ID="ce1" runat="server" PopupButtonID="calImg" Enabled="true" Format="dd/MM/yyyy" TargetControlID="txtReturned" PopupPosition="TopRight" OnClientDateSelectionChanged="AppendTime"></ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("DateTimeReturned","{0:dd/MM/yyyy HH:mm}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="btnCAN" runat="server" CausesValidation="false" CommandName="Cancel" Text="CANCEL" />
<asp:Button ID="btnUPD" runat="server" ValidationGroup="vg1" CausesValidation="true" CommandName="Update" Text="UPDATE" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnEDT" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' Text="SIGN IN" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
The LinkButton btnEDT works and puts the gridview in edit mode. But in code behind I cannot find "txtReturned".
This is what I've tried so far...
protected void gvSignInRegister_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int rowIdx = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvSignInRegister.Rows[rowIdx];
if (row != null && row.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)row.FindControl("txtReturned");
if (tb != null) tb.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
//I've tried this too but it does not work. Interestingly, it does not crash, so cells[4] must exist!
//row.Cells[4].Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
}
}
}
For some reason the rowIdx is always 0. Why? I thought a row index of 0 meant the header of the gridview control.
I've also tried using the NamingContainer that most other people have suggested in other posts, but that returns a blank (I suspect new?) GridViewRow.
ie
GridViewRow row = (GridViewRow)((GridViewRow)(e.CommandSource).NamingContainer);
UPDATE
I found this, which is exactly the problem I am having, but the solution via the RowEditing still does not find the textbox!
However the RowDataBound() solved it! Read my answer below.
The answer was in getting into the editmode version of the GridView itself and then find the control!
As per this post...
<asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3"
DataSourceID="sdsSignInRegister" ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" GridLines="Vertical" OnRowDataBound="gvSignInRegister_RowDataBound">
<Columns> ...etc...
protected void gvSignInRegister_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
TextBox tb = (TextBox)e.Row.FindControl("txtReturned");
if (tb != null) tb.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
}
}
}
Use Container.DisplayIndex instead of Container.DataItemIndex
But I dont think you will get textBox control if you are placing it in EditItemTemplate
If your expectation is editing operation then please use the below code
HTML
<asp:GridView ID="gvSignInRegister" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" ForeColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" OnRowEditing="gvSignInRegister_RowEditing" OnRowCancelingEdit="gvSignInRegister_RowCancelingEdit" OnRowUpdating ="gvSignInRegister_RowUpdating" GridLines="Vertical">
<Columns>
<asp:TemplateField HeaderText="Returned" SortExpression="DateTimeReturned">
<EditItemTemplate>
<asp:TextBox ID="txtReturned" Text='<%#Bind("DateTimeReturned", "{0:dd/MM/yyyy HH:mm}")%>' runat="server"></asp:TextBox>
<asp:ImageButton runat="Server" ID="calImg" ImageUrl="~/images/Calendar_scheduleHS.png" AlternateText="Click to show calendar" CausesValidation="False" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval( "DateTimeReturned","{0:dd/MM/yyyy HH:mm}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
Code Behind:
protected void gvSignInRegister_RowEditing(object sender, GridViewEditEventArgs e)
{
gvSignInRegister.EditIndex = e.NewEditIndex;
List<QuotationDetail> itemList = (List<QuotationDetail>)ViewState["ItemList"];
gvSignInRegister.DataSource = itemList;
gvSignInRegister.DataBind();
}
protected void gvSignInRegister_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
protected void gvSignInRegister_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var txtQty = (TextBox)gvSignInRegister.Rows[e.RowIndex].FindControl("txtQuantity");
decimal qty = 0;
decimal.TryParse(txtQty.Text, out qty);
if (qty < 0)
{
lblErrorSummary.InnerText = "Please provide valid Quantity";
lblErrorSummary.Visible = true;
return;
}
itemList[e.RowIndex].Quantity = qty
ViewState["ItemList"] = itemList;
gvSignInRegister.EditIndex = -1;
gvSignInRegister.DataSource = itemList;
gvSignInRegister.DataBind();
}

How can i detect the Link button fire command-button in gridview control

I have a Strange issue that is i have develop a web application in one of the form i am using Gridview control with command button to view the row data.If in Gridview data have only record then link button working fine if it is more than one record not working link buttons i can't under stand, this is very strange issue to me i have use !Page.IsPostBack also in pageload event,please help me .....
protected void grdmanageloans_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Info")
{
try
{
int loanid = Convert.ToInt32(e.CommandArgument);
Session["Loanid"] = loanid;
Session["Edit"] = "Edit";
TabContainer1.ActiveTabIndex = 1;
Session["TabContainer1"] ="loantab";
Session["Tabloan"] = "Tabloan";
Response.Redirect("Mortgageclient.aspx");
}
catch { }
}
if (e.CommandName == "Delete")
{
try
{
int LoanId = Convert.ToInt32(e.CommandArgument);
var PmtScheduleHistory = from del in mortgageentity.Pmt_Schedule_History where del.Loan.Loan_ID == LoanId select del;
var LoanPayment = from del in mortgageentity.Payments where del.Loan_ID == LoanId select del;
if (PmtScheduleHistory.Count() > 0)
{
var DelPmtScheduleHistory = (from del in mortgageentity.Pmt_Schedule_History where del.Loan.Loan_ID == LoanId select del).First();
mortgageentity.DeleteObject(DelPmtScheduleHistory);
mortgageentity.SaveChanges();
}
var Getpayments = from db in mortgageentity.Payments where db.Loan_ID == LoanId select db;
if (Getpayments.Count() > 0)
{
foreach (var i in Getpayments)
{
mortgageentity.DeleteObject(i);
mortgageentity.SaveChanges();
}
}
if (LoanPayment.Count() > 0)
{
var DelPmtScheduleHistory = (from del in mortgageentity.Payments where del.Loan_ID == LoanId select del.Payment_Status.PaymentStatus_ID).First();
mortgageentity.DeleteObject(DelPmtScheduleHistory);
mortgageentity.SaveChanges();
}
var deletedata = (from del in mortgageentity.Loans where del.Loan_ID == LoanId select del).First();
mortgageentity.DeleteObject(deletedata);
mortgageentity.SaveChanges();
BindData();
}
catch { }
}
if (e.CommandName == "AddNewloan")
{
Session["Addnewloan"] = "Addloan";
Response.Redirect("Information.aspx");
}
}
Here is my .aspx page
<asp:GridView ID="grdmanageloans" runat="server" AutoGenerateColumns="False" AllowSorting="True"
GridLines="Both" PageSize="10" AllowPaging="true" OnRowCommand="grdmanageloans_RowCommand"
OnSelectedIndexChanged="grdmanageloans_SelectedIndexChanged" ShowFooter="true"
OnPageIndexChanging="grdmanageloans_PageIndexChanging" OnRowDataBound="grdmanageloans_Rowdatabound">
<AlternatingRowStyle BackColor="#F3F9FB" />
<RowStyle BackColor="#FEFEFE" VerticalAlign="Top" />
<HeaderStyle BackColor="#F3F9FB" />
<FooterStyle BackColor="#F3F9FB" />
<RowStyle Wrap="False" />
<HeaderStyle ForeColor="#1F476F" />
<Columns>
<asp:TemplateField HeaderText="LoanID" Visible="true">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblloanid" runat="server" Text='<%# Bind("Loan_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loan Number">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblLoanNumber" runat="server" Text='<%# Bind("LoanNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Month Pay Amt" HeaderStyle-Wrap="false">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblMonthPayAmt" runat="server" Text='<%#Getammount(Eval("MonthPayAmt","{0:F2}")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblpropaddress" runat="server" Text='<%# Bind("PropAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblpropcity" runat="server" Text='<%# Bind("PropCity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblpropstate" runat="server" Text='<%# Bind("PropState") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ClientID" Visible="false">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblclientid" runat="server" Text='<%# Bind("Client_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lnkeinfo" runat="server" CausesValidation="false" CommandName="Info"
CommandArgument='<%#Eval("Loan_ID")%>'>Information</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try this:
<asp:LinkButton ID="lnkeinfo" runat="server" CausesValidation="false" CommandName="Info"
CommandArgument='<%# Bind("Loan_ID") %>'>Information</asp:LinkButton>
Why don't you use a RESTful architecture to not save those parameters in Session and make pages kind'ev tightly coupled? Just create links named Info in your Grid instead of buttons (which post back data) with those parameters appended to the end of them as querystring. This is nicer approach and the result is the same.

Categories

Resources