asp.net code
<asp:GridView ID="GridView1" Width="100%" runat="server" CssClass=" table table-bordered" RowStyle-HorizontalAlign="Center" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="Field office">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>--Select Field--</asp:ListItem>
<asp:ListItem>Ahmedabad</asp:ListItem>
<asp:ListItem>Ahmedabad-2</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
<asp:ListItem>Bangalore-2</asp:ListItem>
<asp:ListItem>Bangalore-3</asp:ListItem>
<asp:ListItem>Bhubaneshwar</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Cochin</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Guwahati</asp:ListItem>
<asp:ListItem>Guwahati-2</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
<asp:ListItem>Indore</asp:ListItem>
<asp:ListItem>Jaipur</asp:ListItem>
<asp:ListItem>Kolhapur</asp:ListItem>
<asp:ListItem>Kolkata</asp:ListItem>
<asp:ListItem>Lucknow</asp:ListItem>
<asp:ListItem>Ludhiana</asp:ListItem>
<asp:ListItem>Mumbai 1</asp:ListItem>
<asp:ListItem>Mumbai 2</asp:ListItem>
<asp:ListItem>Madurai</asp:ListItem>
<asp:ListItem>Nagpur</asp:ListItem>
<asp:ListItem>Patna</asp:ListItem>
<asp:ListItem>Pune</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Field Center ">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Segment">
<ItemTemplate>
<%--<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SS ">
<ItemTemplate>
<%--<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>--%>
<%--<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Productivity">
<ItemTemplate>
<%--<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>--%>
<%--<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<%--<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>--%>
<%--<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
C# code
int subrows1 = Convert.ToInt32(Session["seg"].ToString());
int rows1 = Convert.ToInt32(Session["fno"].ToString());
try
{
for (int i = 0; i < rows1; i++)
{
DropDownList dl = (DropDownList)GridView1.Rows[i].Cells[0].FindControl("DropDownList1");
TextBox tx = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
string a = tx.Text;
string b = dl.SelectedItem.Text;
for (int j = 0; j < subrows1; j++)
{
TextBox txt = (TextBox)GridView1.Rows[j].FindControl("txt" + j);
//Label l = (Label)GridView1.Rows[i].Cells[2].FindControl("lbl" + j);
//TextBox t = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txt" + j);
//TextBox t1 = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txt1" + j);
//TextBox t2 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txt2" + j);
Label l = (Label)GridView1.Rows[i].FindControl("lbl0");
TextBox t = (TextBox)GridView1.Rows[i].FindControl("txt0");
TextBox t1 = (TextBox)GridView1.Rows[i].FindControl("txt1" + j + "_" + j);
TextBox t2 = (TextBox)GridView1.Rows[i].FindControl("txt2" + j + "_" + j);
}
}
Related
I have 2 dropdownlist using Cascading method. But everytime the 1st item of 2nd dropdownlist gets updated in the database even if choose 2nd or 3rd item from the dropdownlist.
Selection from 2nd Dropdownlist
If I view my Database, it shows the Frame Hoist updated in Reason column instead of Number Punching m/c
Database image of updated data after selection
Please find below my .cs code
namespace Loss_Portal
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
populateData();
}
}
private void populateData()
{
// here code for populate person data
using (TrialEntities20 dc = new TrialEntities20())
{
var v = (from p in dc.Vehicle_Andon
where (p.Line_Name == DropDownList1.SelectedValue
&& p.Loss_Type == 9 && p.Date_Time == Date1.Value)
join s in dc.LOSS_TYPE_MASTER on p.Loss_Description equals s.Loss_Description
select new
{
p,
s.Loss_Description,
});
List<Vehicle_Andon> allP = new List<Vehicle_Andon>();
foreach (var i in v)
{
Vehicle_Andon p = new Vehicle_Andon();
p = i.p;
p.Loss_Description = i.Loss_Description;
allP.Add(p);
}
GridView1.DataSource = allP;
GridView1.DataBind();
}
}
private List<LOSS_TYPE_MASTER> populateState()
{
// here code for populate state
List<LOSS_TYPE_MASTER> all = new List<LOSS_TYPE_MASTER>();
using (TrialEntities20 dc = new TrialEntities20())
{
all = dc.LOSS_TYPE_MASTER.ToList();
}
return all;
}
public class ClassName
{
public string Station { get; set; }
public string Reason { get; set; }
}
private List<ClassName> populateCity(int Loss_Type)
{
List<ClassName> obj = new List<ClassName>();
using (TrialEntities20 dc = new TrialEntities20())
if (Loss_Type == 1)
{
obj = (from p in dc.LINE_CONFIGRATOR
where p.Line_Name == DropDownList1.SelectedValue && p.STATION == L2.Text
select new ClassName
{
Station = p.STATION,
Reason = p.Reason
}).ToList();
}
else if (Loss_Type == 6)
{
obj = (from p in dc.LINE_SKILL_CONFIG
where p.Line_Name == DropDownList1.SelectedValue && p.STATION == L2.Text
select new ClassName
{
Station = p.STATION,
Reason = p.Reason
}).ToList();
}
else if (Loss_Type == 8)
{
obj = (from p in dc.MATERIAL_SHORTAGE
where p.Line_Name == DropDownList1.SelectedValue && p.STATION == L2.Text
select new ClassName
{
Station = p.STATION,
Reason = p.Reason
}).ToList();
}
return obj;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string loss_type = GridView1.DataKeys[e.NewEditIndex]["Loss_Type"].ToString();
string station = GridView1.DataKeys[e.NewEditIndex]["Station"].ToString();
// Edit Event
GridView1.EditIndex = e.NewEditIndex;
populateData();
// Here populate data for dropdown state
DropDownList ddLoss_DescriptionEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddLoss_Description");
if (ddLoss_DescriptionEdit != null)
{
ddLoss_DescriptionEdit.DataSource = populateState();
ddLoss_DescriptionEdit.DataTextField = "Loss_Description";
ddLoss_DescriptionEdit.DataValueField = "Loss_Type";
ddLoss_DescriptionEdit.DataBind();
ddLoss_DescriptionEdit.SelectedValue = loss_type;
}
string V = (GridView1.Rows[e.NewEditIndex].FindControl("txtStation") as Label).Text.Trim();
L2.Text = V;
DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("ddReason");
if (ddReasonEdit != null)
{
ddReasonEdit.DataSource = populateCity(Convert.ToInt32(loss_type));
ddReasonEdit.DataTextField = "Reason";
ddReasonEdit.DataValueField = "Station";
ddReasonEdit.DataBind();
ddReasonEdit.SelectedValue = station;
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
populateData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Here Update
int Master_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Master_ID"].ToString());
DropDownList ddLoss_DescriptionEdit = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddLoss_Description");
DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddReason");
Label txtStationEdit = (Label)GridView1.Rows[e.RowIndex].FindControl("txtStation");
using (TrialEntities20 dc = new TrialEntities20())
{
Vehicle_Andon p = dc.Vehicle_Andon.Where(a => a.Master_ID.Equals(Master_ID)).FirstOrDefault();
if (p != null)
{
p.Loss_Type = Convert.ToInt32(ddLoss_DescriptionEdit.SelectedValue);
p.Loss_Description = ddLoss_DescriptionEdit.SelectedItem.ToString();
p.Reason = ddReasonEdit.SelectedItem.ToString();
dc.SaveChanges();
GridView1.EditIndex = -1;
populateData();
}
}
}
protected void ddLoss_Description_SelectedIndexChanged(object sender, EventArgs e)
{
// Populate city
{
string Loss_Type = ((DropDownList)sender).SelectedValue;
int rowIndex = GridView1.EditIndex;
DropDownList ddReasonEdit = (DropDownList)GridView1.Rows[rowIndex].FindControl("ddReason");
if (ddReasonEdit != null)
{
var v = populateCity(Convert.ToInt32(Loss_Type));
ddReasonEdit.DataSource = v;
ddReasonEdit.DataBind();
if (ddReasonEdit.Items.Count > 0)
{
ddReasonEdit.SelectedIndex = 0;
}
}
}
}
}
}
html code
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h3>Loss Portal</h3>
<div style="margin-left: 300px">
<asp:Label ID="Label1" runat="server" Text="Select Date :" Font-Bold="True" Font-Names="Arial Black" Font-Overline="False"></asp:Label>
<input id="Date1" runat="server" type="date" /><asp:RequiredFieldValidator ID="requiredfieldvalidator" runat="server" ControlToValidate="Date1" ErrorMessage="Date is Required" ForeColor="Red" style="z-index: 1; top: 109px; position: absolute; left: 720px" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="Arial Black" Text="Select Line :"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Line_Name" DataValueField="Line_Name" Height="28px" Width="211px">
</asp:DropDownList><asp:RequiredFieldValidator ID="requiredfieldvalidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Select Line" ForeColor="Red" style="z-index: 1; top: 154px; position: absolute; left: 720px" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TrialConnectionString2 %>" SelectCommand="SELECT DISTINCT [Line_Name] FROM [Vehicle_Andon] ORDER BY [Line_Name]"></asp:SqlDataSource>
<br />
<br />
<asp:Button ID="Button1" runat="server" CssClass="btn-primary-active" OnClick="Button1_Click" Text="Submit" Width="180px" Height="31px" />
<br />
<br />
<asp:Label ID="L2" runat="server" Visible="false"/>
<br />
</div>
<div style="margin-left: 0px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" onrowediting="GridView1_RowEditing" DataKeyNames="Loss_Type,Station,Master_ID"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdating="GridView1_RowUpdating" GridLines="None" ForeColor="#333333" >
<AlternatingRowStyle BackColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:TemplateField HeaderText="Master_ID " HeaderStyle-Width="30px" HeaderStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label Text='<%# Eval("Master_ID")%>' runat="server" Width="70px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtMaster_ID" Text='<%# Eval("Master_ID")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Width="30px"></HeaderStyle>
<HeaderStyle HorizontalAlign="Center" Width="30px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Station No." HeaderStyle-Wrap="false" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Station")%>' runat="server" Width="82px" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtStation" Text='<%# Eval("Station")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Justify"></HeaderStyle>
<HeaderStyle HorizontalAlign="Center" Width="30px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Conveyor No." HeaderStyle-Wrap="false" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Conveyor")%>' runat="server" Width="80px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtConveyor" Text='<%# Eval("Conveyor")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Andon_PC " HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Andon_PC")%>' runat="server" Width="75px" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtAndon_PC" Text='<%# Eval("Andon_PC")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start_Time " HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Start_Time")%>' runat="server" Width="200px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtStart_Time" Text='<%# Eval("Start_Time")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Close_Time " HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Close_Time")%>' runat="server" Width="200px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtClose_Time" Text='<%# Eval("Close_Time")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Duration")%>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtDuration" Text='<%# Eval("Duration")%>' runat="server" Width="55px"/>
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Line_Name" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Line_Name")%>' runat="server" Width="155px" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtLine_Name" Text='<%# Eval("Line_Name")%>' runat="server" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loss_Description" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="ltype" Text='<%# Eval("Loss_Description")%>' runat="server" Width="150px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropdownList ID="ddLoss_Description" DataTextField="Loss_Descripiton" DataValueField="Loss_Type"
runat="server" AutoPostBack="true" OnSelectedIndexchanged="ddLoss_Description_SelectedIndexChanged" >
</asp:DropdownList>
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label Text='<%# Eval("Reason")%>' runat="server" Width="100px" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropdownList ID="ddReason" runat="server" DataTextField="Reason" DataValueField="Station">
</asp:DropdownList>
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="120px">
<ItemTemplate>
<asp:ImageButton ImageUrl="imgs/edit.png" runat="server" CommandName="Edit" CommandArgument='<%#Eval("Master_ID") %>' ToolTip="Edit" Width="25px" Height="25px" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="imgs/save.png" runat="server" CommandName="Update" ToolTip="Update" CommandArgument='<%#Eval("Master_ID") %>' Width="25px" Height="25px" />
<asp:ImageButton ImageUrl="imgs/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="25px" Height="25px" />
</EditItemTemplate>
<HeaderStyle VerticalAlign="Middle" Wrap="False" HorizontalAlign="Center"></HeaderStyle>
</asp:TemplateField>
</Columns>
<EditRowStyle BorderColor="#99CCFF" BorderStyle="Dotted" HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#3399FF" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#003366" BorderStyle="Solid" Width="50px" Wrap="False" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" BorderColor="#003366" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" BorderStyle="Dotted" HorizontalAlign="Center" VerticalAlign="Middle" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" HorizontalAlign="Center" VerticalAlign="Middle" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</div>
</asp:Content>
I'm new to Linq query and I need to complete my project.
Please, could someone help ?
How to get the row index of a dynamically created asp.net GridView so that I can edit the record in a text box outside the GridView.
How to get the row index of a dynamically created asp.net GridView so that I can edit the record in a text box outside the GridView.
This is the GridView:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="RefID">
<ItemTemplate>
<asp:Label ID="lbl_Refid" runat="server" Text='<%# Eval("Refid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbl_date" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name Of Company">
<ItemTemplate>
<asp:Label ID="lbl_noc" runat="server" Text='<%# Eval("Name_Of_Company") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact/Email">
<ItemTemplate>
<asp:Label ID="lbl_wht_do" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:Label ID="lbl_wht_do" runat="server" Text='<%# Eval("Remarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btn_edit" runat="server" `enter code here` Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand = "OnRowCommand">
Use
OnRowCommand = "OnRowCommand"
Now when any action happen on the grid the OnRowCommand event is executed.
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
More Info
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="MyButtonClick" />
</ItemTemplate>
and your method
protected void MyButtonClick(object sender, System.EventArgs e)
{
//Get the button that raised the event
Button btn = (Button)sender;
//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
}
I want to check for the services in TypeOfServices according to the ClientId, and check for all the services.
The database Columns are TaxId, ClientId, ClientName,TypeOfservice
The gridview should display Client names in alphabetical order,and the services present should display 'yes' else 'no'
I used this C# code but it's not working...
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblclientname = (Label)e.Row.FindControl("lblclientname");
Label lblincometax = (Label)e.Row.FindControl("lblincometax");
Label lblprofessionaltax = (Label)e.Row.FindControl("lblprofessionaltax");
Label lblservicetax = (Label)e.Row.FindControl("lblservicetax");
Label lbltds = (Label)e.Row.FindControl("lbltds");
Label lblvat = (Label)e.Row.FindControl("lblvat");
Label lblcompanylaw = (Label)e.Row.FindControl("lblcompanylaw");
string m = "SELECT * FROM tblTaxMaster";
ds = gs.getdata(m);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows.Count > 0)
{
string ClientId = ds.Tables[0].Rows[0]["ClientId"].ToString();
string TaxId = ds.Tables[0].Rows[0]["TaxId"].ToString();
string ser = "select TypeOfService from tblTaxMaster where ClientId= '" + ClientId.ToString() + "' and TaxId = '" + TaxId.ToString() + "' ";
ds = gs.getdata(ser);
if (ser == "INCOME TAX")
{
lblincometax.Text = "YES";
}
else
{
lblincometax.Text = "NO";
}
if (ser == "TDS")
{
lbltds.Text = "YES";
}
else
{
lbltds.Text = "NO";
}
if (ser == "PROFESSIONAL TAX")
{
lblprofessionaltax.Text = "YES";
}
else
{
lblprofessionaltax.Text = "NO";
}
if (ser == "SERVICE TAX")
{
lblservicetax.Text = "YES";
}
else
{
lblservicetax.Text = "NO";
}
if (ser == "VAT")
{
lblvat.Text = "YES";
}
else
{
lblvat.Text = "NO";
}
if (ser == "COMPANY LAW")
{
lblcompanylaw.Text = "YES";
}
else
{
lblcompanylaw.Text = "NO";
}
}
}
}
}
This is the ASP.Net Code
<asp:GridView ID="GridView1" runat="server" Width="550px"
AutoGenerateColumns="False" Font-Names="Cambria" Font-Size="10pt"
ShowFooter="True" FooterStyle-BackColor="#FF5600"
CellPadding="4" CssClass="grid-view" PageSize="20"
GridLines="None" ForeColor="#333333" onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Client Name">
<ItemTemplate>
<asp:Label ID="lblclientname" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Income Tax">
<ItemTemplate>
<asp:Label ID="lblincometax" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Professional Tax">
<ItemTemplate>
<asp:Label ID="lblprofessionaltax" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Service Tax">
<ItemTemplate>
<asp:Label ID="lblservicetax" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="TDS">
<ItemTemplate>
<asp:Label ID="lbltds" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="VAT">
<ItemTemplate>
<asp:Label ID="lblvat" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Company Law">
<ItemTemplate>
<asp:Label ID="lblcompanylaw" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="">
<ItemTemplate>
<asp:Button ID="btnservicedetails" runat="server" Text="Service Details" />
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#333333" />
<FooterStyle BackColor="#191919" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#191919" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E6E6E6" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Why are looping if you are already getting the required values from
SELECT * FROM tblTaxMaster
When bind the gridview use joins and get the TypeOfService value in the select statement only and then modify your source code to
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Client Name">
<ItemTemplate>
<asp:Label ID="lblclientname" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="Income Tax">
<ItemTemplate>
<asp:Label ID="lblincometax" runat="server" Text='<%#Eval("typeOfService")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
In my Asp.net App, i have a GridView and i generate the data of column[6] by myself using code behind.
by looking at the code below, i have a footer for my gridview. and the problem is my text for column[6] won't appear if i use footer.
If i delete the footertext code, then my text for column[6] is appear. what is the problem? both of the code cant use togather? i already set ShowFooter="True"
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < (this.GridView1.Rows.Count); i++)
{
this.GridView1.Rows[i].Cells[6].Text = "testing";
//GridView1.Columns[1].FooterText ="footer 1";
}
}
.aspx
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
ForeColor="#333333" GridLines="None" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="reportDate" HeaderText="Report Date" dataformatstring="{0:dd MMMM yyyy}" SortExpression="reportDate" />
<asp:BoundField DataField="sponsorBonus" HeaderText="Sponsor Bonus" dataformatstring="{0:0.00}" SortExpression="sponsorBonus" HtmlEncode="False" />
<asp:BoundField DataField="pairingBonus" HeaderText="Pairing Bonus" HtmlEncode="False" SortExpression="pairingBonus" dataformatstring="{0:c}" />
<asp:BoundField DataField="staticBonus" HeaderText="Static Bonus" SortExpression="staticBonus" />
<asp:BoundField DataField="leftBonus" HeaderText="Left Bonus" SortExpression="leftBonus" />
<asp:BoundField DataField="rightBonus" HeaderText="Right Bonus" SortExpression="rightBonus" />
<asp:BoundField HeaderText="Total" SortExpression="total" >
<ItemStyle Width="100px" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Sample Code: To set Footer text programatically
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal.ToString("c");
}
}
UPDATED CODE:
decimal sumFooterValue = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sponsorBonus = ((Label)e.Row.FindControl("Label2")).Text;
string pairingBonus = ((Label)e.Row.FindControl("Label3")).Text;
string staticBonus = ((Label)e.Row.FindControl("Label4")).Text;
string leftBonus = ((Label)e.Row.FindControl("Label5")).Text;
string rightBonus = ((Label)e.Row.FindControl("Label6")).Text;
decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus);
e.Row.Cells[6].Text = totalvalue.ToString();
sumFooterValue += totalvalue;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = sumFooterValue.ToString();
}
}
In .aspx Page
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
ForeColor="#333333" GridLines="None" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="Report Date" SortExpression="reportDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("reportDate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("reportDate", "{0:dd MMMM yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sponsor Bonus" SortExpression="sponsorBonus">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("sponsorBonus") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("sponsorBonus", "{0:0.00}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pairing Bonus" SortExpression="pairingBonus">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("pairingBonus") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("pairingBonus", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Static Bonus" SortExpression="staticBonus">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("staticBonus") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("staticBonus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Left Bonus" SortExpression="leftBonus">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("leftBonus") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("leftBonus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Right Bonus" SortExpression="rightBonus">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("rightBonus") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("rightBonus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" SortExpression="total">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotal" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
protected void gvBill_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
Total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "InvMstAmount"));
else if (e.Row.RowType == DataControlRowType.Footer)
e.Row.Cells[7].Text = String.Format("{0:0}", "<b>" + Total + "</b>");
}
This can be achieved through LINQ with grouping, here a list of items pointed as a data source to the actual grid view.
Sample pseudo code which could help coding the actual.
var tabelDetails =(from li in dc.My_table
join m in dc.Table_One on li.ID equals m.ID
join c in dc.Table_two on li.OtherID equals c.ID
where //Condition
group new { m, li, c } by new
{
m.ID,
m.Name
} into g
select new
{
g.Key.ID,
Name = g.Key.FullName,
sponsorBonus= g.Where(s => s.c.Name == "sponsorBonus").Count(),
pairingBonus = g.Where(s => s.c.Name == "pairingBonus").Count(),
staticBonus = g.Where(s => s.c.Name == "staticBonus").Count(),
leftBonus = g.Where(s => s.c.Name == "leftBonus").Count(),
rightBonus = g.Where(s => s.c.Name == "rightBonus").Count(),
Total = g.Count() //Row wise Total
}).OrderBy(t => t.Name).ToList();
tabelDetails.Insert(tabelDetails.Count(), new //This data will be the last row of the grid
{
Name = "Total", //Column wise total
sponsorBonus = tabelDetails.Sum(s => s.sponsorBonus),
pairingBonus = tabelDetails.Sum(s => s.pairingBonus),
staticBonus = tabelDetails.Sum(s => s.staticBonus),
leftBonus = tabelDetails.Sum(s => s.leftBonus),
rightBonus = tabelDetails.Sum(s => s.rightBonus ),
Total = tabelDetails.Sum(s => s.Total)
});
<asp:TemplateField HeaderText="ExEmp" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
FooterStyle-BackColor="BurlyWood" FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtNoOfExEmp" runat="server" CssClass="form-control input-sm m-bot15"
Font-Bold="true" onkeypress="return isNumberKey(event)" Text='<%#Bind("ExEmp") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="50px" />
<FooterTemplate>
<asp:Label ID="lblTotNoOfExEmp" Font-Bold="true" runat="server" Text="0" CssClass="form-label"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
private void TotalExEmpOFMonth()
{
Label lbl_TotNoOfExEmp = (Label)GrdPFRecord.FooterRow.FindControl("lblTotNoOfExEmp");
/*Sum of the Total Amount Of month*/
foreach (GridViewRow gvr in GrdPFRecord.Rows)
{
TextBox txt_NoOfExEmp = (TextBox)gvr.FindControl("txtNoOfExEmp");
lbl_TotNoOfExEmp.Text = (Convert.ToDouble(txt_NoOfExEmp.Text) + Convert.ToDouble(lbl_TotNoOfExEmp.Text)).ToString();
lbl_TotNoOfExEmp.Text = string.Format("{0:F0}", Decimal.Parse(lbl_TotNoOfExEmp.Text));
}
}
int total = 0;
protected void gvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount"));
}
if(e.Row.RowType==DataControlRowType.Footer)
{
Label lblamount = (Label)e.Row.FindControl("lblTotal");
lblamount.Text = total.ToString();
}
}
/*This code will use gridview sum inside data list*/
SumOFdata(grd_DataDetail);
private void SumOFEPFWages(GridView grd)
{
Label lbl_TotAmt = (Label)grd.FooterRow.FindControl("lblTotGrossW");
/*Sum of the total Amount of the day*/
foreach (GridViewRow gvr in grd.Rows)
{
Label lbl_Amount = (Label)gvr.FindControl("lblGrossS");
lbl_TotAmt.Text = (Convert.ToDouble(lbl_Amount.Text) + Convert.ToDouble(lbl_TotAmt.Text)).ToString();
}
}
I have gridview which contains 60 rows and each row has 4 radiobutton option (select any one). To opearate that code , on submit button wrote code as follows . I didn't work.
My design view :
<asp:GridView ID="gvSurvey" runat="server" CellPadding = "4" OnRowDataBound ="gvSurvey_RowDataBound" BorderWidth ="2"
AutoGenerateColumns="False" EmptyDataText="No data Available" GridLines="None"
HorizontalAlign="Center" ForeColor="#333333" Font-Names="Verdana" ShowFooter="True">
<RowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="HEADER" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="QTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SQTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Disagree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn1" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton1" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Agree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn2" runat ="server" GroupName ="MyRadioGroup"/>
<%--<asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton2" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Disagree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn3" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton3" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Agree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn4" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%-- <input name="MyRadioButton4" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
C#
for (i = 0; i < gvSurvey.Rows.Count; i++)
{
RadioButton rdbtn1 = (RadioButton)gvSurvey.Rows[i].Cells[3].FindControl("rdbtn1");
RadioButton rdbtn2 = (RadioButton)gvSurvey.Rows[i].Cells[4].FindControl("rdbtn2");
RadioButton rdbtn3 = (RadioButton)gvSurvey.Rows[i].Cells[5].FindControl("rdbtn3");
RadioButton rdbtn4 = (RadioButton)gvSurvey.Rows[i].Cells[6].FindControl("rdbtn4");
// RadioButton rdbtn1=(RadioButton) gvSurvey.Rows[i].ClientID
qtn_no = dtSurvey.Rows[i]["QTN_NO"].ToString();
sqtn_no = dtSurvey.Rows[i]["SQTN_NO"].ToString();
if (rdbtn1.Checked)
{
rdbtn1.Text = "1";
lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, rdbtn1.Text);
}
Please Go through the following code you will find the Solutions
// It will be on your submit button click
protected void getPinCode()
{
foreach (GridViewRow grdRows in gvSurvey.Rows)
{
RadioButton rbt1 = (RadioButton)grdRows.FindControl("rdbtn1");
RadioButton rbt2 = (RadioButton)grdRows.FindControl("rdbtn2");
RadioButton rbt3 = (RadioButton)grdRows.FindControl("rdbtn3");
RadioButton rbt4 = (RadioButton)grdRows.FindControl("rdbtn4");
string Value = RadioValue(rbt1);
//Similarly you can do for all radio button
if(!string.IsNullOrEmpty(Value))
{
lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, Value);
}
}
}
protected string RadioValue(RadioButton Rbtlst)
{
string Value = "";
if (Rbtlst.Checked == true)
{
Value = Rbtlst.Text;
}
return Value;
}