Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have a grid view having two button fields as template field with some others fields. I want to get the two cells values of gridview on click event of one button. My code is as follows -
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
DataKeyNames="ID" >
        <columns>
            <asp:boundfield headertext="UID" datafield="ID" />
            <asp:boundfield headertext="Complaint ID" datafield="Complaint_ID" />
            <asp:boundfield headertext="Type of solution" datafield="TOS" />
            <asp:boundfield headertext="Present Complaint" datafield="Present_Complaint" />
            <asp:boundfield headertext="Disease Name" datafield="Disease_Name" />
            <asp:boundfield headertext="Previous Test" datafield="Investigation" />
            <asp:boundfield headertext="Geneticals Problems" datafield="Genetical" />
            <asp:boundfield headertext="Exercise" datafield="Exercise" />
            <asp:boundfield headertext="Addiction" datafield="Addiction" />
            
            <asp:templatefield>
            <itemtemplate>
            <asp:button id="btn_view" runat="server" text="View" onclick="btn_view_click" />
            </itemtemplate>
            </asp:templatefield>
            <asp:templatefield>
            <itemtemplate>
            <asp:button id="btn_download" runat="server" text="Download" onclick="btn_download_click" />
            </itemtemplate> 
            </asp:templatefield>
        </columns>
    
    </asp:gridview>

I had tried this on click event of view buttons like this.
protected void btn_view_click(object sender, EventArgs ee)
      {
          GridViewRow grow = GridView1.Rows[0];
          TableCell tc = grow.Cells[2];

      }
Posted
Updated 9-Jul-10 22:48pm
v2

Have a look at this link:
http://bytes.com/topic/asp-net/answers/725154-get-cell-value-within-gridview[^]
Discussion on similar thing.
 
Share this answer
 
Comments
mcapatna 10-Jul-10 5:07am    
Thanks Sandeep,
this is the same problem like me.but unfortunately there was no solution on that forum.
mcapatna 10-Jul-10 9:57am    
Reason for my vote of 5
exact understanding of question. although i can't get my solution but what i want to say he understand exactly and tried to solve my question.
go to events tab of gridview double click on rowcommand event it will generate the rowcommand event for gridview. and now for CommandArgument of the button write something like this

CommandArgunment='<%#Eval("Column1") + "~" + Eval("Column2") %>'

now in the rowcommand event
string value = e.CommandArgument.ToString();
str[] values = value.Split['~'];

str[0] and str[1] will contain both of the column1 and column2 values respectively.
 
Share this answer
 
Comments
mcapatna 10-Jul-10 10:06am    
Reason for my vote of 4
for quick reply
mcapatna 10-Jul-10 10:28am    
thanks Priya Jul2010 sorry for disturbing you again all things are ok but the last statement produced the error i'e str[] values=value.split['~'];
please help me.
raju melveetilpurayil 12-Jul-10 14:25pm    
mcapatna check this
string[] values = value.Split(new char[] { '~' });
See here.
 
Share this answer
 
Comments
mcapatna 10-Jul-10 5:11am    
Thanks Abhinav,
i had already tried that site but the solution given there is not working correctly. i cannot understand the meaning of 'e'.
aswathy.s.88 10-Jul-10 7:48am    
e is the event handler...
mcapatna 10-Jul-10 10:02am    
thanks aswathy.s.88,
will you please refer that forum ?i might be wrong but on that forum e sense other thing like variable.
the easy way for you would be for the button write the commandname as select and handle the selected index changed event of gridview and from the selectedrow property you can get the values of the columns you need.

Eg: GridView1.SelectedRow.Cells[2].Text
 
Share this answer
 
Comments
Shining Legend 10-Jul-10 10:47am    
what is the error?
Shining Legend 10-Jul-10 10:48am    
check if the e.CommandArgument has any value or not by debugging.
Shining Legend 10-Jul-10 10:51am    
I guess here is the mistake:

values = value.Split('~'); //need to be paranthesis not square brackets.
hi,

u can try these codes.

In html page:
XML
<asp:GridView runat=server ID=GridView6  CellPadding=3 CellSpacing=0 AutoGenerateColumns=false>
  <Columns>
   <asp:TemplateField HeaderText="Column1">
     <ItemTemplate>
     <asp:Button runat=server ID=btn_View OnClick="btn_View_Click"  Text="View" />
     </ItemTemplate>
   </asp:TemplateField>
  <asp:BoundField DataField="Column1" HeaderText="Column1" />
     <asp:BoundField DataField="Column2" HeaderText="Column2" />
     <asp:BoundField DataField="Column3" HeaderText="Column3" />
  </Columns>
  </asp:GridView>


In code Pgae:
C#
protected void btn_View_Click(object sender, EventArgs e)
        {
            int rowIndex = (((sender as Button).NamingContainer) as GridViewRow).RowIndex;
            GridViewRow GridViewRow1 = this.GridView6.Rows[rowIndex];
            //......

        }


Hope help u.
 
Share this answer
 
Instead of trying to figure out the row index of the button, just use the CommandArgument property in addition to the GridView event bubbling. Then you can go to your datasource for all you want to know about the record in question.


XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField HeaderText="name" DataField="name"/>
        <asp:TemplateField>
            <ItemTemplate>
            <asp:Button id="Button1" runat="server" Text="View" CommandName="MyView" CommandArgument='<%#Eval("id") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


and in code behind

C#
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    String id = e.CommandArgument.ToString();
    if (e.CommandName == "View")
    {
        //process the id
    }
}
 
Share this answer
 

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