Link button inside nested gridview invoking the wrong row - c#

I have a expand collapse nested grid view like below.
The childgrid (gvChild) column is a linkbutton column on the click of which I need to get the name of the city which was clicked. My rowcommand event to capture that on the child gridview is like this:
protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ShowDetails")
{
int index = Convert.ToInt32(e.CommandArgument);
GridView gridChild = (GridView)gvwParent.Rows[index].FindControl("gvChild");
foreach (GridViewRow row in gridChild .Rows)
{
string aCity= ((LinkButton)row.Cells[1].FindControl("lnkSelectedCity")).Text;
}
}
}
I understand the findcontrol line is def wrong, but how else would I do the findcontrol of the child grid to get the right link button that was clicked? Right now if I select "London", the Index value is 1 and the String "aCity" is taking it as "Utah". Will appreciate inputs on this. Thank you.
Code on my aspx page:
<asp:GridView ID="gvParent" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="CountryID"
OnRowDataBound="gvParent_RowDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javaScript:divexpandcollapse('div<%# Eval("CountryID") %>');">
<img id='imgdiv<%# Eval("CountryID") %>' width="9px" border="0" src="expand_white.gif"
alt="" /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryDesc">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CountryDesc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id='div<%# Eval("CountryID") %>' style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="False" PageSize="5"
Width="100%" Height="270px" ShowHeader="False" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:BoundField DataField="CityID" HeaderText="CityID">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelectedCity" runat="server" Text='<% # Eval("CityName") %>'
CommandName="ShowDetails" CommandArgument='<%# Container.DataItemIndex %>' ToolTip="Select x"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Assigned" HeaderText="Assigned">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="CountryID" HeaderText="Assigned">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ClassID" HeaderText="ClassID">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="UOM" HeaderText="UOM">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</td></tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I assume CityID is a unique key. If so, you can assign CityID to CommandArgument.
Then you can retrieve the CityID back from CommandArgument after post back.
<asp:GridView ID="gvChild" ... DataKeyNames="CityID">
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelectedCity"
...
CommandArgument='<%# Eval("CityID") %>'
ToolTip="Select x" />
</ItemTemplate>
</asp:TemplateField>
protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ShowDetails")
{
int cityId = Convert.ToInt32(e.CommandArgument);
}
}

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;

ASP.Net GridView unsorted afrer PostBack

I have a custom GridView in asp.net Page. In PageLoad data will be bound to the grid.
protected void Page_Load(Object sender, EventArgs e)
{
InitDisplayMode(sender);
}
private void InitDisplayMode(Object sender)
{
pnlDetails.Visible = SafetyKeyId != -1;
if (!Page.IsPostBack)
{
BindValues();
}
// some other stuff
}
private void BindValues()
{
MyStrongTypedDataTable tbl = new BusinessObject.GetData();
dataGrid.DataSource = tbl;
dataGrid.DataBind();
}
Grid View Makrup
<asp:GridView ID="dataGrid" runat="server" AutoGenerateColumns="False" OnRowCreated="OnRowCreated"
AllowSorting="true" BorderWidth="0" CellPadding="2" CssClass="subitem w100p"
AllowPaging="true" PageSize="25" RowStyle-Wrap="false" HeaderStyle-Wrap="false"
OnPageIndexChanging="PageGrid" OnRowDataBound="dataGrid_RowDataBound" PagerSettings-Mode="Numeric"
PagerSettings-Position="Bottom" OnSorting="SortGrid" OnRowDeleting="RowDeleting">
<RowStyle CssClass="itemRowStyle"/>
<AlternatingRowStyle CssClass="alternatingItemRowStyle"/>
<HeaderStyle CssClass="headerRowStyle"/>
<Columns>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:HyperLink ID="lnkDetail" runat="server" meta:resourcekey="lnkDetail" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_file.png" %>'
NavigateUrl="~/BrandMgmt/BrandMgmtPublicKey.aspx?"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:HyperLink ID="lnkEdit" runat="server" meta:resourcekey="lnkEdit" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_edit.png" %>'
NavigateUrl="~/BrandMgmt/BrandMgmtPublicKey.aspx?"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:ImageButton ID="cmdDelete" Width="16" Height="16" runat="server" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_delete.png" %>'
CommandName="Delete" CommandArgument='<%# Eval("SafetyKeyId") %>' OnCommand="OnDelete"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="KeyOwnerIdentifier" HeaderText="KeyOwnerIdentifier" meta:resourcekey="ColumnKeyOwnerIdentifier"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="KeyOwnerIdentifier"/>
<asp:BoundField DataField="SafetyKeyName" HeaderText="SafetyKeyName" meta:resourcekey="ColumnSafetyKeyName"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="SafetyKeyName"/>
<asp:BoundField DataField="SafetyKeyTypeId" HeaderText="SafetyKeyTypeId" meta:resourcekey="ColumnSafetyKeyTypeId"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="SafetyKeyTypeId"/>
<asp:BoundField DataField="KeyIndex" HeaderText="KeyIndex" meta:resourcekey="ColumnKeyIndex"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="KeyIndex"/>
<asp:BoundField DataField="FromDate" HeaderText="FromDate" meta:resourcekey="ColumnFromDate"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="FromDate"/>
<asp:BoundField DataField="ToDate" HeaderText="ToDate" meta:resourcekey="ColumnToDate"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="ToDate"/>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle"
meta:resourcekey="ColumnPublicKey" SortExpression="PublicKey">
<ItemTemplate>
<div style="max-width: 200px; overflow-y: scroll;">
<asp:Label runat="server" Text='<%# Eval("PublicKey") %>'/>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Grid hat 3 ImageButtons for detail view, editing and deleting per row. DetaliView Button adds additional Table with data to the Page. Editing makes this Table editable. If the grid is sorted and any of those buttons is beeing clicked, Page loads and any sorting is lost.
How can I maintain the sort setting in PostBack after clicking any of those buttons?

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();
}
}

How to disable LinkButton within GridViewclick from Client Side using JavaScript?

I have a GridView, In one column I have LinkButton control. I want to disable click from client side for certain condition on this column. Means for some rows it will not be possible for User to call onclick event and for some rows it is possible.
I want to achieve this from client side using javascript.
Or When User clicks on link, It will notify the User that this action can't be completed for this row.
<asp:GridView Width="100%" ShowHeader="true" ViewStateMode="Enabled" GridLines="Both" CellPadding="10" CellSpacing="5" ID="GridViewMultiplePOSAssociationId" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="RewardGridMultiD_RowCommand"
AllowPaging="true" PageSize="8" OnRowDataBound="grdViewCustomers_OnRowDataBound" PagerSettings-Position="Top" PagerSettings-Mode="NumericFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" DataKeyNames="POS Id">
<RowStyle CssClass="table_inner_text" BackColor="WhiteSmoke" BorderColor="CornflowerBlue" Wrap="true" ForeColor="Black" Height="30px" />
<HeaderStyle CssClass="table_head_text" />
<Columns>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval(" POS Id ") %>');">
<img alt="Details" id="imgdiv<%# Eval("POS Id") %>" src="images/plus.png" />
</a>
<div id="div<%# Eval(" POS Id ") %>" style="display: none;">
<asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<RowStyle CssClass="table_inner_text" BackColor="SkyBlue" BorderColor="Black" Wrap="true" ForeColor="White" Height="30px" />
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="RULE FILE NAME" HeaderText="RULE FILE NAME" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE ID" HeaderText="RULE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE TYPE ID" HeaderText="RULE TYPE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="START TIME" HeaderText="START TIME" />
<asp:BoundField ItemStyle-Width="150px" DataField="EXPIRY TIME" HeaderText="EXPIRY TIME" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Row Number">
<ItemTemplate>
<asp:Label ID="LabelRowNumberId" runat="server" Text='<%#Eval("Row Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="POS Id">
<ItemTemplate>
<asp:Label ID="LabelPOSID" runat="server" Text='<%#Eval("POS Id") %>'></asp:Label>
<%-- <asp:LinkButton ID="LinkbtnPOSId" CommandArgument='<%#Eval("POS Id") %>' CommandName="ClickPOS" Text='<%#Eval("POS Id") %>' runat="server" CausesValidation="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>' CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/
<asp:LinkButton ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace" Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="LabelStatusPendingPOSId" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridViewMultiplePOSAssociationId there is one column "Status" which has label LabelStatusPendingPOSId, LabelStatusPendingPOSId text is set Applied, Not Applied at the time of Binding. For Rows which has Status Applied, User should not be able to click on LinkButton Associate/Replace else He is allowed to click.
use this code OnRowDataBound for Your Grid
protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton LinkbtnPOSId=(LinkButton)e.Row.FindControl("LinkbtnPOSId");
Label LabelStatusPendingPOSId = (Label)e.Row.FindControl("LabelStatusPendingPOSId");
Boolean boolStatus = LabelStatusPendingPOSId.Text == "Applied" ? true : false;
//LinkbtnPOSId.Attributes.Add("onclick", "function CheckStatus('" + boolStatus.ToString() + "')");
LinkbtnPOSId.Enabled = boolStatus;
}
}
Try this..
You can loop through the Gridview and make the particular control disable...
I am writing the code in javascript pageload function...
function PageLoad(sender, args)
{
for (var i = 0; i <= RowCount; i++)
{
document.getElementById('Gridview1_HyperLinkAssociate_' + i.toString() + '').disabled = true;
}
}
Set RowCount as the number of rows of the Gridview...

Add check box to gridview

I've a grid view with three columns Employee name Employee details and Employee age.
I want to add a check box at each row and after selection of each check box i want to fire a insert query associated to that employee.
Can you tell me how to add this dynamic functionality to the grid view.
also if we use <%# EVAL %> i don't know how to implement it with checkbox.
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="details" HeaderText="details"
SortExpression="details" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connApps %>"
SelectCommand="SELECT [name], [details], [age] FROM [tblA]">
</asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Selected item name:<br>";
foreach (GridViewRow item in GridView1.Rows)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
Label1.Text += item.Cells[1].Text + "<br>";
}
}
}
}
Output:
Here is an example ,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"
CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
Font-Names="Arial" GridLines="Vertical" Width="40%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server"
AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
<HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />
For more information , check Here !
you can use TemplateFields for the GridView:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and in code behind use below code:
ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")

Categories

Resources