Hi AshiWar,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BoundField bfield = new BoundField();
bfield.HeaderText = "Id";
bfield.DataField = "CustomerId";
GridView1.Columns.Add(bfield);
TemplateField tfield = new TemplateField();
tfield.HeaderText = "Name";
GridView1.Columns.Add(tfield);
tfield = new TemplateField();
tfield.HeaderText = "Country";
GridView1.Columns.Add(tfield);
tfield = new TemplateField();
tfield.HeaderText = "View";
GridView1.Columns.Add(tfield);
}
this.BindGrid();
}
private void BindGrid()
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtName = new TextBox();
txtName.ID = "txtName";
txtName.Text = (e.Row.DataItem as DataRowView).Row["Name"].ToString();
e.Row.Cells[1].Controls.Add(txtName);
DropDownList ddlCountry = new DropDownList();
ddlCountry.ID = "ddlCountry";
ddlCountry.DataSource = GetData();
ddlCountry.DataTextField = GetData().Columns[1].ColumnName;
ddlCountry.DataValueField = GetData().Columns[0].ColumnName;
ddlCountry.DataBind();
string country = (e.Row.DataItem as DataRowView).Row["Country"].ToString();
ddlCountry.Items.FindByText(country).Selected = true;
e.Row.Cells[2].Controls.Add(ddlCountry);
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["CustomerId"].ToString();
e.Row.Cells[3].Controls.Add(lnkView);
}
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT CustomerId,Country,Name FROM Customers";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void ViewDetails(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = (row.FindControl("txtName") as TextBox).Text;
string country = (row.FindControl("ddlCountry") as DropDownList).SelectedItem.Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + "\\n\\rName: " + name + "\\n\\rCountry: " + country + "')", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim bfield As BoundField = New BoundField()
bfield.HeaderText = "Id"
bfield.DataField = "CustomerId"
GridView1.Columns.Add(bfield)
Dim tfield As TemplateField = New TemplateField()
tfield.HeaderText = "Name"
GridView1.Columns.Add(tfield)
tfield = New TemplateField()
tfield.HeaderText = "Country"
GridView1.Columns.Add(tfield)
tfield = New TemplateField()
tfield.HeaderText = "View"
GridView1.Columns.Add(tfield)
End If
Me.BindGrid()
End Sub
Private Sub BindGrid()
GridView1.DataSource = GetData()
GridView1.DataBind()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtName As TextBox = New TextBox()
txtName.ID = "txtName"
txtName.Text = (TryCast(e.Row.DataItem, DataRowView)).Row("Name").ToString()
e.Row.Cells(1).Controls.Add(txtName)
Dim ddlCountry As DropDownList = New DropDownList()
ddlCountry.ID = "ddlCountry"
ddlCountry.DataSource = GetData()
ddlCountry.DataTextField = GetData().Columns(1).ColumnName
ddlCountry.DataValueField = GetData().Columns(0).ColumnName
ddlCountry.DataBind()
Dim country As String = (TryCast(e.Row.DataItem, DataRowView)).Row("Country").ToString()
ddlCountry.Items.FindByText(country).Selected = True
e.Row.Cells(2).Controls.Add(ddlCountry)
Dim lnkView As LinkButton = New LinkButton()
lnkView.ID = "lnkView"
lnkView.Text = "View"
AddHandler lnkView.Click, AddressOf ViewDetails
lnkView.CommandArgument = (TryCast(e.Row.DataItem, DataRowView)).Row("CustomerId").ToString()
e.Row.Cells(3).Controls.Add(lnkView)
End If
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT CustomerId,Country,Name FROM Customers"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub ViewDetails(ByVal sender As Object, ByVal e As EventArgs)
Dim lnkView As LinkButton = (TryCast(sender, LinkButton))
Dim row As GridViewRow = (TryCast(lnkView.NamingContainer, GridViewRow))
Dim id As String = lnkView.CommandArgument
Dim name As String = (TryCast(row.FindControl("txtName"), TextBox)).Text
Dim country As String = (TryCast(row.FindControl("ddlCountry"), DropDownList)).SelectedItem.Text
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Id: " & id & "\n\rName: " & name & "\n\rCountry: " & country & "')", True)
End Sub
Screenshot
