Hi ahmadsubuhanl...,
Please refer below sample.
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 runat="server" ID="gvCustomers" AutoGenerateColumns="false" OnRowDataBound="gvCustomers_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="CustomerId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownCheckBoxes runat="server" ID="ddlCountries" Width="150px" UseSelectAllNode="false">
<Style SelectBoxWidth="150" DropDownBoxBoxWidth="150" DropDownBoxBoxHeight="90" />
</asp:DropDownCheckBoxes>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Saplin.Controls;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports Saplin.Controls
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownCheckBoxes ddlCountries = e.Row.FindControl("ddlCountries") as DropDownCheckBoxes;
ddlCountries.Items.Clear();
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
ddlCountries.Items.Add(new ListItem(dr["Country"].ToString(), dr["CustomerId"].ToString()));
}
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub gvCustomers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlCountries As DropDownCheckBoxes = TryCast(e.Row.FindControl("ddlCountries"), DropDownCheckBoxes)
ddlCountries.Items.Clear()
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
For Each dr As DataRow In dt.Rows
ddlCountries.Items.Add(New ListItem(dr("Country").ToString(), dr("CustomerId").ToString()))
Next
End Using
End Using
End Using
End Using
End If
End Sub
Screenshot