Hey akhter,
Please refer below sample.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DropDownList ID="ddCustomerId" runat="server" OnSelectedIndexChanged="GetData"
AutoPostBack="true">
</asp:DropDownList>
<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="ShipVia" HeaderText="ShipVia" />
<asp:BoundField DataField="Freight" HeaderText="Freight" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT top 5 CustomerId From Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
ddCustomerId.DataSource = dt;
ddCustomerId.DataTextField = "CustomerId";
ddCustomerId.DataValueField = "CustomerId";
ddCustomerId.DataBind();
ddCustomerId.Items.Insert(0, new ListItem("Select CustomerId", "0"));
}
}
}
}
}
protected void GetData(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT c.CustomerId, c.ContactName,ShipVia,Freight From Orders o INNER JOIN Customers c on c.CustomerId =o.CustomerId WHERE c.CustomerId =@CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", ddCustomerId.SelectedItem.Value);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
dt = new DataTable();
dt.Columns.Add("Amount");
da.Fill(dt);
GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double shipvia = Convert.ToDouble(e.Row.Cells[2].Text);
double freight = Convert.ToDouble(e.Row.Cells[3].Text);
double total = shipvia * freight;
e.Row.Cells[4].Text = total.ToString();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT top 5 CustomerId From Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
ddCustomerId.DataSource = dt
ddCustomerId.DataTextField = "CustomerId"
ddCustomerId.DataValueField = "CustomerId"
ddCustomerId.DataBind()
ddCustomerId.Items.Insert(0, New ListItem("Select CustomerId", "0"))
End Using
End Using
End Using
End If
End Sub
Protected Sub GetData(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT c.CustomerId, c.ContactName,ShipVia,Freight From Orders o INNER JOIN Customers c on c.CustomerId =o.CustomerId WHERE c.CustomerId =@CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", ddCustomerId.SelectedItem.Value)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
dt = New DataTable()
dt.Columns.Add("Amount")
da.Fill(dt)
GridView1.DataSource = dt
Me.GridView1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim shipvia As Double = Convert.ToDouble(e.Row.Cells(2).Text)
Dim freight As Double = Convert.ToDouble(e.Row.Cells(3).Text)
Dim total As Double = shipvia * freight
e.Row.Cells(4).Text = total.ToString()
End If
End Sub
Screenshot