Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have some data in datatable i want to retrieve a row from datatable based on value

for example
the data in datatable like
1 prasad hyderabad b.tech cse 2015

if i enter the any 1 or prasad in textbox i want to display the row in gridview how i can i achieve this one please guys

thanks
Posted

GridView1.DataSource=dt;  // dt is Your DataTable
GridView1.DataBind();
 
Share this answer
 
based upon the below query you can search the data in table and display it to the grid view
C#
DataTable objdt = new DataTable();


var searchval=Textbox1.Text

C#
string query =@ "select * from TableName where name like '%" + searchval+ "%' or id like'%" + searchval+ "%' 

C#
SqlDataAdapter da = new SqlDataAdapter(query, con);

C#
con.Open();
 da.Fill(objdt);
 con.Close();
 if (objdt.Rows.Count > 0)
 {
 GridView1.DataSource = objdt;
 GridView1.DataBind();
 }
 
Share this answer
 
Hello ,

You can use LINQ to extract any row from the datatable

C#
DataRow[] result = table.Select("Size >= 230);


"Size" in above line refer to Column name.

just need to define the condition in table.select("Condition")
Once you get the row array you can apply loop over it.


C#
foreach (DataRow row in result)
	{
	    Console.WriteLine("{0}, {1}", row[0], row[1]);
	}



Thanks.
Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900