display the data in gridview based on dropdownlist selected value

Posted by Ramakrishna188 under ASP.NET on 3/1/2012 | Points: 10 | Views : 22951 | Status : [Member] | Replies : 5
HI all,
Please send me any one . my requirement is in one form i have gridview and one dropdownlist control i want to bind same table with dropdownlist control and gridview control. i want to display the data in gridview based on selected value in the dropdownlist control.
Thank u.




Responses

Posted by: Anweshabhowmick on: 3/1/2012 [Member] Starter | Points: 25

Up
0
Down
1First get the value of the dropdown list.

string s= DropdownList1.SelectedValue.Tostring();

2. The send this value to your Query.

private void BindData()

{
SqlConnection con = new SqlConnection("YOUR CONNECTION STRING");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM YOUR TABLE where YOUR FIELD NAME= s" , con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

}


Try this code this will helpful to u...

and inform it will work or not.................


Ramakrishna188, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja_89 on: 3/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hai
How can i display data in grid view based on values selected on two drop down list box [dates]
Regards




Ramakrishna188, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Bind the gridview based on the dropdownlist selected values(date).
For example:

public void BindGrid()

{
//sqlconnection
String Query ="select * from tablename where columnname between Convert.ToDateTime(dropdownlist1Id.SelectedItmem) and
Convert.ToDateTime(dropdownlist2Id.SelectedItmem) ;
//execute the query.
//populate the grid by these values.
}

//call the above the method where you need. like from any events


Thanks & Regards,
Santhi .V

Ramakrishna188, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/11/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Database Table

CREATE TABLE [dbo].[StudentAdmission](
[AdmissionNumber] [varchar](20) NULL,
[StudentName] [varchar](20) NULL,
[StudentRollNumber] [int] NULL,
[RegisteredDate] [datetime] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Stored Procedure

create proc GetDatabetweenDates
(
@FromDate datetime,
@todate datetime
)
as
begin
select * from StudentAdmission where RegisteredDate between @FromDate and @todate
end


HTML Mark up

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DNFAnswer.aspx.cs" Inherits="DNFAnswer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 44%;
}
.style2
{
}
.style3
{
width: 74px;
}
.style4
{
width: 94px;
}
.style5
{
width: 138px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<table class="style1">
<tr>
<td class="style4">
From Date</td>
<td class="style5">
<asp:TextBox ID="xtxtFromDate" runat="server"></asp:TextBox>
</td>
<td class="style3">
To Date</td>
<td>
<asp:TextBox ID="xtxttoDate" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" colspan="4">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
</td>
</tr>
</table>

</div>
<div align="center">
<asp:gridview ID="xgvDetails" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="StudentName" HeaderText="Name" />
<asp:BoundField DataField="RegisteredDate" HeaderText="Registered Date" />
</Columns>
</asp:gridview>
</div>
</form>
</body>
</html>



Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class DNFAnswer : System.Web.UI.Page
{
string strconn = ConfigurationManager.ConnectionStrings["cnnlocal"].ToString();
SqlConnection sqlcon = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)

{

}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
BindGrid();
}


private void BindGrid()
{
DateTime fromdate = Convert.ToDateTime(xtxtFromDate.Text);
DateTime todate = Convert.ToDateTime(xtxttoDate.Text);
sqlcon = new SqlConnection(strconn);
sqlcon.Open();
SqlCommand cmd = new SqlCommand("GetDatabetweenDates", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FromDate", SqlDbType.DateTime).Value = fromdate;
cmd.Parameters.AddWithValue("@todate", SqlDbType.DateTime).Value = todate;
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
xgvDetails.DataSource = ds;
xgvDetails.DataBind();
}

}
}


Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Ramakrishna188, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
Ram Try this code

protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
if (!IsPostBack)
{
Dt = ReturnDt("Select * from dbo.Table_1");
DrpStudentName.DataSource = Dt;
DrpStudentName.DataTextField = "StudentName";
DrpStudentName.DataValueField = "StudentId";
DrpStudentName.DataBind();

}
}


public DataTable ReturnDt(string RecStr)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = RecStr;
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(Dt);
return Dt;
}

void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Select * from dbo.Table_1";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
GridView1.DataSource = ds;
GridView1.DataMember = "student";
GridView1.DataBind();
con.Close();
}


protected void DrpStudentName_SelectedIndexChanged(object sender, EventArgs e)
{
Dt = ReturnDt("Select * from dbo.Table_1 where StudentId="+ Convert.ToInt32(DrpStudentName.SelectedValue) +"");
GridView1.DataSource = Dt;
GridView1.DataBind();
}



Mark as Answer if its helpful to you

Ramakrishna188, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response