How to select distinct values in gridview
i have grid view like this
namr mobile area
raju 90165432 hyd
ramu 98493131 hyd
raju 90165432 hyd
jki 98493131 hyd
kasd 90165432 hyd
when i select options all... select only unique mobile numbrs only ..not duplicate mobile numbers
raju 90165432 hyd
ramu 98493131 hyd
Loading

Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 17, 2013, 4:36 AM EDT
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Gridview
{
public partial class WebForm2 : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select DISTINCT * from emp where name in('raju','ramu')";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "emp");
GridView1.DataMember = "emp";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
}
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 17, 2013, 7:30 PM EDT
hi,
As driscrib above, you have to change your query, put DISTINCT or group by clause in your query.
Select DISTINCT name,mobile,area from Table1
Or
Select name,mobile,area from Table1
Group by name,mobile,area
hope this will help you.