GridView with RadioButton binding and Show corresponding row..


Here I explained about Gridview with RadioButton operation Like IRCTC website for select one option ,it will show corresponding records. Here using gridview inside control template for view RadioButton.OnCheckedChanged=TestChanged() event send to radiobutton.Using RadioButton rb = new RadioButton();rb = (RadioButton)(sender);

Database Connection:


Below coding used to Connect Database Connection in C#
 public void DatabaseConnection()
{
string str;
//str = ConfigurationManager.AppSettings["DBconnection"].ToString();
str = "server=xxxx;Database=SqlWorking;uid=sa;pwd=sa;";
sqlconn = new SqlConnection(str);
sqlconn.Open();
}


or we can write the code One class file or inside C# page.. Here I explained about Database coding...
"using System.Data.SqlClient" namespace used to enabled some connection method..

DBConnection.cs File




using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public class DBconnection
{
string str;
SqlConnection sqlconn;
SqlCommand cmd;
SqlDataAdapter sqlada;
DataSet ds;

public void connectingstring()
{
string str;
str = "server=xxxx;Database=SqlWorking;uid=sa;pwd=sa;";
sqlconn = new SqlConnection(str);
sqlconn.Open();
}
public void query(string str)
{
connectingstring();
cmd = new SqlCommand(str,sqlconn);
cmd.ExecuteNonQuery();
}
public void fetchquery(string str)
{
connectingstring();
sqlada = new SqlDataAdapter(str,sqlconn);
ds = new DataSet();
sqlada.Fill(ds,"xxxx");
}
public void Bindgrid()
{
connectingstring();
sqlada = new SqlDataAdapter(str, sqlconn);
ds = new DataSet();
sqlada.Fill(ds,"xxxx");

}
}



Here Aspx coding for Radio button article. In GridView subcontrol using Radio Button.
Then OnCheckedChanged="TestChanged"Event used for RadioButton select option.

Employee.aspx file



Note:In aspx coding replace < tag or > instead of --


--asp:GridView ID="GridView2" runat="server" BorderColor="#D2DFA8" Height="16px" OnRowDataBound="GridView2_RowDataBound"
wrap="true" Width="100%" BorderStyle="Double" AllowPaging="True" OnPageIndexChanging="GridView2_PageIndexChanging"
PageSize="5" AllowSorting="True" OnSorting="GridView2_Sorting"--
--PagerSettings Mode="NumericFirstLast" --/
--Columns--
--asp:TemplateField HeaderText="Select"--
--ItemTemplate--
--asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" OnCheckedChanged="TestChanged" --/
--/ItemTemplate--
--/asp:TemplateField--
--/Columns--
--HeaderStyle BackColor="#9BAF38" BorderColor="#D2DFA8" BorderStyle="Solid" --/
--/asp:GridView--



Main aspx.cs file


Here cs file explained, We can Invoke DBconnection.cs Class file through Using New object "DBconnection DB = new DBconnection();"
Then Bindgrid() method used to Bind the GridView using GridView2.DataBind();


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
DBconnection DB = new DBconnection();

Calling DBconnection.cs class


GridView Binding method:




public void Bindgrid()
{
DatabaseConnection();//if we need...
string str;
str = "select * from Emp_Table;";
DB.fetchquery(str);
sqlada = new SqlDataAdapter(str, sqlconn);
ds1 = new DataSet();
sqlada.Fill(ds1, "xxxxx");
GridView2.DataSource=ds1.Tables[0];
GridView2.DataBind();

}



RadioButton function in Inside Gridview Control.



calling OnCheckedChanged="TestChanged" from aspx page to cs page method.

Sender event send OnCheckedChanged event


RadioButton rb = new RadioButton();
rb = (RadioButton)(sender);


Complete Radiobutton sender event coding inside Gridview control



protected void TestChanged(object sender, EventArgs e)
{
LblMessage.Text = "";

string m_ClientID = "";
RadioButton rb = new RadioButton();
rb = (RadioButton)(sender);
m_ClientID = rb.ClientID;
foreach (GridViewRow row in GridView2.Rows)
{
rb = (RadioButton)row.FindControl("RadioButton1");
rb.Checked = false;
if (m_ClientID == rb.ClientID)
{
rb.Checked = true;
}
if (rb.Checked == true)
{

string str;
Id = Convert.ToInt32(GridView2.Rows[row.RowIndex].Cells[1].Text);
str = "select Employee_Id,Ename,Marital_status,Blood_groub,Address from Emp_Table ";
DB.query(str);
DB.fetchquery(str);
cmd = new SqlCommand(str, sqlconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmpID", SqlDbType.Int);
cmd.Parameters["@EmpID"].Value = Id;
cmd.ExecuteNonQuery();
sqlada = new SqlDataAdapter(str, sqlconn);
ds1 = new DataSet();
sqlada.Fill(ds1, "xxx");
GridView3.DataSource = ds1.Tables[0];
GridView3.DataBind();
Bindgrid();
}
}
}


OutPut File see below the image file..



If we select any one radiobutton , selected corresponding row details. Above green color coding mentioned one radiobutton selected at one time..


Attachments

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: