This article will show you how you can retrieve distinct
value from a datatable using c#.net. in this I will show you to get the
distinct values of the datatable column.
Some of my previous articles related to gridview are as
follows: Page
Count Display in GridView Footer In Asp.Net Using C#.Net, GridView
Bind Using DataTable With Paging In Asp.Net Using C#.Net, Single
RadioButton Selection in GridView In Asp.Net Using C#.Net, Bind
and Validate GridView TextBox Value by jQuery In Asp.Net Using C#, Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#, Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Search
and Display Data In GridView From Database Table In Asp.Net Using C#.Net, Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Search
GridView Record on Button Click By Using C#.Net in Asp.Net.
So for this article first we will create a new application and
retrieve data from database in a datatable using c#.net and vb.net.
But first please check the table data.
SqlConnection con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
DataView
objdbview = new DataView(objdt);
DataTable
objdistinctitem = objdbview.ToTable(true, "UserType");
}
}
catch
{
con.Close();
}
}
|
Here in above code please check the highlighted code. This is used for retrieving the distinct
value.
DataView
objdbview = new DataView(objdt);
DataTable
objdistinctitem = objdbview.ToTable(true, "UserType");
|
Now here is the output of the code.
Now we will retrieve distinct value on the bases of multiple
column. Please check the modified code. First please check the sql table data.
Now check the code
DataView objdbview = new DataView(objdt);
DataTable objdistinctitem = objdbview.ToTable(true, "UserType",
"Name");
|
Now check the output.
0 comments:
Please let me know your view