Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a gridview in that I have two buttons on click event I have to find out Datakey value and values of each cells.
Following is the code in ASPX
XML
<asp:GridView ID="GRVLeaveRequest" runat="server" AutoGenerateColumns="False"
            CellPadding="4" Font-Names="Verdana" Font-Size="X-Small" ForeColor="#000099"
            GridLines="None" DataKeyNames="OfficialEmail">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField HeaderText="Employee Name" ItemStyle-HorizontalAlign="Center" DataField="EmployeeName" />
                <asp:BoundField HeaderText="Date From" ItemStyle-HorizontalAlign="Center" DataField="Startdate" />
                <asp:BoundField HeaderText="Date To" ItemStyle-HorizontalAlign="Center" DataField="Endate" />
                <asp:BoundField HeaderText="No of Leave Available" ItemStyle-HorizontalAlign="Center" DataField="Leaves" />
                <asp:TemplateField HeaderText="Approve">
                <ItemTemplate>
                    <asp:Button ID="btnApprove" Height="25px" ForeColor="Navy" Font-Size="X-Small" OnClick="btnApprove_Click" Font-Names="Verdana" runat="server" Text="Approve" />
                </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Dis-Approve">
                <ItemTemplate>
                    <asp:Button ID="btnDisApprove" Height="25px" ForeColor="Navy" Font-Size="X-Small" OnClick="btnDisApprove_Click" Font-Names="Verdana" runat="server" Text="Dis-Approve" />
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <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>



//*********************************//
and in code code behind did something like it
C#
protected void btnApprove_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow grd in GRVLeaveRequest.Rows)
        {
/*Here I have to find out datakey value and each cells value*/
        }
    }



Please help me in sorting out my problem .I will be very thankful to you
People.


Thanks in advance.
Posted
Comments
Hiren solanki 4-Jan-11 4:58am    
See my updated answer this may help.
Hiren solanki 4-Jan-11 6:35am    
accept answer if it helped. or make subject [solved] accordingly. please

for finding cell values of that row you need to find container row of button first.

See THIS[^] will surely help you.

[update]

As you now got object of GridViewRow(grdRow) from linked article.

you could further investigate like below to find datakey on Button click event

GridViewName.DataKeys[grdRow.RowIndex].Value.toString();


you do not need to loop through each row if button is present for every row.
 
Share this answer
 
v2
Comments
Mohd Wasif 4-Jan-11 4:53am    
Thanks for your response but I need to find out data key value.
How I will get it in button click event ?
Please let me know
TejuKrishna 4-Jan-11 5:53am    
write event handler for your button
btnApprove_Click(object sender, EventArgs e)
{
//your code here
}
Mohd Wasif 4-Jan-11 5:58am    
Thanks problem has been solved out.
Dalek Dave 4-Jan-11 6:34am    
Good Answer, Hiren.
foreach (GridViewRow row in GridView1.Rows)
{
int _dkname = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);


}
 
Share this answer
 
Comments
Sandeep Mewara 4-Jan-11 5:49am    
? No explaination?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900