Hi kankon,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
DataKeyNames="CustomerId" OnRowDataBound="gvCustomers_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="CustomerId">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("CustomerId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" Font-Size="Large" Width="100%"
AutoPostBack="true" OnSelectedIndexChanged="Update">
<asp:ListItem Value="section0" Text="Please select"></asp:ListItem>
<asp:ListItem Value="section1" Text="Excellent"></asp:ListItem>
<asp:ListItem Value="section2" Text="VeryGood"></asp:ListItem>
<asp:ListItem Value="section3" Text="Good"></asp:ListItem>
<asp:ListItem Value="section4" Text="Poor"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Drawing
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Select CustomerId,Name,Country From Customers", con))
{
con.Open();
gvCustomers.DataSource = cmd.ExecuteReader();
gvCustomers.DataBind();
con.Close();
}
}
}
public void Update(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
if (ddl.SelectedIndex > 0)
{
GridViewRow gvRow = (sender as DropDownList).NamingContainer as GridViewRow;
string id = (gvRow.FindControl("lblId") as Label).Text.Trim();
string ddlcountry = ddl.SelectedItem.Text;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Country = @Country WHERE CustomerId = @CustomerId", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerId", id);
if (!string.IsNullOrEmpty(ddlcountry))
{
cmd.Parameters.AddWithValue("@Country", ddlcountry);
}
else
{
cmd.Parameters.AddWithValue("@Country", DBNull.Value);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
BindGrid();
}
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != gvCustomers.EditIndex)
{
if (gvCustomers.DataKeys[e.Row.RowIndex].Values[0].ToString() == "Excellent")
{
e.Row.BackColor = Color.White;
e.Row.ForeColor = Color.Black;
}
else if (gvCustomers.DataKeys[e.Row.RowIndex].Values[0].ToString() == "Very Good")
{
e.Row.BackColor = Color.SkyBlue;
e.Row.ForeColor = Color.Black;
if (gvCustomers.DataKeys[e.Row.RowIndex].Values[0].ToString() == "Good")
{
e.Row.BackColor = Color.Pink;
e.Row.ForeColor = Color.Black;
}
else if (gvCustomers.DataKeys[e.Row.RowIndex].Values[0].ToString() == "Poor")
{
e.Row.BackColor = Color.Yellow;
e.Row.ForeColor = Color.Black;
}
}
}
}
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 constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("Select CustomerId,Name,Country From Customers", con)
con.Open()
gvCustomers.DataSource = cmd.ExecuteReader()
gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Public Sub Update(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = TryCast(sender, DropDownList)
If ddl.SelectedIndex > 0 Then
Dim gvRow As GridViewRow = TryCast((TryCast(sender, DropDownList)).NamingContainer, GridViewRow)
Dim id As String = (TryCast(gvRow.FindControl("lblId"), Label)).Text.Trim()
Dim ddlcountry As String = ddl.SelectedItem.Text
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Country = @Country WHERE CustomerId = @CustomerId", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerId", id)
If Not String.IsNullOrEmpty(ddlcountry) Then
cmd.Parameters.AddWithValue("@Country", ddlcountry)
Else
cmd.Parameters.AddWithValue("@Country", DBNull.Value)
End If
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
BindGrid()
End Sub
Protected Sub gvCustomers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex <> gvCustomers.EditIndex Then
If gvCustomers.DataKeys(e.Row.RowIndex).Values(0).ToString() = "Excellent" Then
e.Row.BackColor = Color.White
e.Row.ForeColor = Color.Black
ElseIf gvCustomers.DataKeys(e.Row.RowIndex).Values(0).ToString() = "Very Good" Then
e.Row.BackColor = Color.SkyBlue
e.Row.ForeColor = Color.Black
If gvCustomers.DataKeys(e.Row.RowIndex).Values(0).ToString() = "Good" Then
e.Row.BackColor = Color.Pink
e.Row.ForeColor = Color.Black
ElseIf gvCustomers.DataKeys(e.Row.RowIndex).Values(0).ToString() = "Poor" Then
e.Row.BackColor = Color.Yellow
e.Row.ForeColor = Color.Black
End If
End If
End If
End Sub
Output
Screenshot