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

Posted by Kumartyr under ASP.NET on 10/26/2013 | Points: 10 | Views : 1496 | Status : [Member] | Replies : 3
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);
}




Responses

Posted by: Bandi on: 10/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down

refer
http://forums.asp.net/t/1880980.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: WPF_G on: 11/18/2013 [Member] Starter | Points: 25

Up
0
Down
If you want to know more about Visual Studio you can download here a FREE eBook : http://www.tekkiebooks.com/campaign/freebook/dotnetfunda_07
LEARN step by step how to install Visual Studio 2012 on Windows 8. Use it as a quick reference or a standard how to document for your development team. Simplify your work and gain time!

Good luck in creating amazing applications!

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/19/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
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.

<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Verdana"
PageSize="5" Width="55%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"
DataKeyNames="Emp_id">
<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" />
<Columns>
<asp:BoundField DataField="Emp_id" HeaderText="Employee ID" Visible="false" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Emp_job" HeaderText="Job title" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Emp_Dep" HeaderText="Department" HeaderStyle-Width="150px">
<HeaderStyle Width="150px"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="40px">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" GroupName="test" AutoPostBack="true" OnCheckedChanged="RadioButton1_CheckedChanged"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<br />
<asp:Label ID="Label1" runat="server" Text="Select Value :"></asp:Label>
</div>


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

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

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



Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response