Hi,
Add Text box in your form.user enter the search string in the text box.
if value matched show that row in first.i hope it will help you
sample is here:-)
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataset.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 10; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;
}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
}
private void SearchGrid(int text)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.Equals(text) ||
dataGridView1.Rows[i].Cells[1].Value.Equals(text))
{
//Swapping Rows
object temp = this.dataGridView1.Rows[i].Cells[0].Value;
object temp1 = this.dataGridView1.Rows[i].Cells[1].Value;
this.dataGridView1.Rows[i].Cells[0].Value = this.dataGridView1.Rows[i].Cells[0].Value;
this.dataGridView1.Rows[i].Cells[1].Value = this.dataGridView1.Rows[i].Cells[1].Value;
this.dataGridView1.Rows[0].Cells[0].Value = temp;
this.dataGridView1.Rows[0].Cells[1].Value = temp1;
}
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
SearchGrid(Convert.ToInt32(textBox1.Text));
}
Best Regards,
C.Gnanadurai
-----------------------
Please mark the post as answer if it is helpfull to you