Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Select rows using keyboard in DataGridView
 

Select rows using keyboard in DataGridView

I have a DataGridView that is displaying data, i have set the selection mode to full row.

Since that data is very large I want to help the user navigate throught the grid using the keyboard, by typing the value of some column while the focus is on the grid (like you navigate in windows explorer), I want that column whose value is compared against the typed one to be the column that is currently sorted with.

Please let me know if there is an easy way to do this.
Ahmad Hajou  Tuesday, September 08, 2009 8:38 AM
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
  • Proposed As Answer byGnanadurai Wednesday, September 09, 2009 5:15 AM
  • Marked As Answer byAhmad Hajou Wednesday, September 09, 2009 6:07 AM
  •  
Gnanadurai  Tuesday, September 08, 2009 10:17 AM
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
  • Proposed As Answer byGnanadurai Wednesday, September 09, 2009 5:15 AM
  • Marked As Answer byAhmad Hajou Wednesday, September 09, 2009 6:07 AM
  •  
Gnanadurai  Tuesday, September 08, 2009 10:17 AM
10x 4 the concern Gnanadurai I managed to add the below code
The code you provided was very helpful
, but i still need to fine tune it a bit:

private void fgvResult_KeyPressedDown(object sender, KeyEventArgs e) {
    try {
        GridKeyDown(e);
    }
    catch (Exception ex) {
        HandleException(ex);
    }
}

private void SearchGrid(string text) {
    for (int i = 0; i < fgvResult.Rows.Count - 1; i++) {
        if ((fgvResult.Rows[i].Cells[0].Value.ToString()).StartsWith(text)) {
            fgvResult.Rows[i].Selected = true;
            return;
        }

    }
}

private void GridKeyDown(KeyEventArgs e) {
    char c = (char)e.KeyValue;

    if(!Char.IsLetterOrDigit(c)) {
        return;
    }

    if(text == null) {
        text = string.Empty;
    }

    TimeSpan span = DateTime.Now - lastKeyTime;

    if(span < new TimeSpan(0, 0, 5)) {
        text += c;
    }
    else {
        text = c.ToString();
    }

    lastKeyTime = DateTime.Now;
    SearchGrid(text);
}

I still need more fine tuning regarding the selection in the grid.

10x for the insight
Ahmad Hajou  Wednesday, September 09, 2009 6:07 AM

You can use google to search for other answers

Custom Search

More Threads

• receive of class udpclient in vs 2003...
• DataGrid Word Wrap
• Allowing the user to choose the system dsn
• how to edit datagrid?
• What must be implemented to bind a control to an object in the propertygrid of the control
• Login failed for user ''(null)''. Reason: Not associated wi...
• DatagridView With multicolumn Combobox
• Problems with DataGridView vs DataSet
• Find System Date
• Cell Formatting Error : Value cannot be null. Parameter name: font