Home > Forum > ASP.NET > How to get a selected column's data in a string from the gridview

How to get a selected column's data in a string from the gridview

by Thamil   on Oct 27, 2013   Category: ASP.NET  |  Views: 12881    |  Points: 5   |  Gold 
i am binding data in Gridview control in page load event

how to get a selected column's data in a string from the Gridview control

(ie)It is the primary key of that table,so for every row selected by user there will be a primary key ID column bounded to it..... so i need to get that ID & with this ID, i need to link with the next page of my web application

using Visual Studio 2005 asp.net C# 2.0

below is some of the code what i tried

protected void SubmitButton_Click(object sender, EventArgs e)
string bS = Convert.ToString(gridview1.Rows[rowIndex].Cells[2].Text);

Question posted on behalf of kumartyr

Bookmark and Share:
  User Reply  | Ask a question  |   Reply 
  Re :How to get a selected column's data in a string from the gridview   
by Thamil
on Oct 27 2013 4:25AM
Points : 10
Hello Kumar,

Here im using employee detail for this sample, first i loaded the gridview with some test data and added radio button in template column and configured the DatakeyName as Emp id in grdview.

Then when you select the radio button in gridview, i have cleared the existing selected radio buttons and get the datakey value of currently selected row in the gridview and stored into seesion varable. By using this session variable on next page and get the gridview datakey value for further process.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Verdana"
PageSize="5" Width="55%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"
<AlternatingRowStyle BackColor="#E0E0E0" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Height="30px" BackColor="#7FC9FF" Font-Size="15px" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" />
<asp:BoundField DataField="Emp_id" HeaderText="Employee ID" Visible="false" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
<asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
<asp:BoundField DataField="Emp_job" HeaderText="Job title" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
<asp:BoundField DataField="Emp_Dep" HeaderText="Department" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
<asp:TemplateField ItemStyle-Width="40px">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<asp:RadioButton ID="RadioButton1" GroupName="test" AutoPostBack="true" OnCheckedChanged="RadioButton1_CheckedChanged"
runat="server" />
<br />
<asp:Label ID="Label1" runat="server" Text="Select Value :"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
private void LoadData()
DataTable dt = new DataTable();
DataRow rw3 = dt.NewRow();
rw3["Emp_id"] = "2700";
rw3["Emp_Name"] = "Muthu kumar";
rw3["Emp_job"] = "Support Enginner";
rw3["Emp_Dep"] = "IT";
DataRow rw4 = dt.NewRow();
rw4["Emp_id"] = "4678";
rw4["Emp_Name"] = "Arvind";
rw4["Emp_job"] = "Sr Software Engineer";
rw4["Emp_Dep"] = "IT";
DataRow rw5 = dt.NewRow();
rw5["Emp_id"] = "2300";
rw5["Emp_Name"] = "Raja ram";
rw5["Emp_job"] = "Test Engineer";
rw5["Emp_Dep"] = "IT";
GridView1.DataSource = dt;

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
int i=0;
foreach(GridViewRow aee in GridView1.Rows)
RadioButton rd = (RadioButton)GridView1.Rows.FindControl("RadioButton1");

RadioButton rdo = (RadioButton)sender;
rdo.Checked = true;
GridViewRow gr = (GridViewRow)rdo.Parent.Parent;
string strValue = GridView1.DataKeys[gr.RowIndex].Value.ToString();
Session["SelVal"] = strValue;
Label1.Text = "Select Value :" + strValue;

I hope you find this helpful to achieve your requirement.

Recent Post

Latest Posts