我们可以在网格视图中上下记录 [英] can we up and down record in grid view
本文介绍了我们可以在网格视图中上下记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以使用向上和向下箭头键在网格视图中上下记录而无需关注或选择记录..
我试过很多但是找不到解决方案..pls给我正确的代码
can we up and down record in grid view using up and down arrow key without focusing or selecting the record..
I tried many bt cant find the solution..pls give me the proper code
推荐答案
是的,但你需要处理文本框的keydown或keypress事件。
示例:文本框的keydown事件。
Yes, but you need to handle the keydown or keypress event for the textbox.
Example: keydown event for the textbox.
private void TextBox_KeyDown(object sender, KeyEventArgs e) {
if (sender == textBox1) { //TextBox
//Adjust Indexes
if (e.KeyCode == Keys.Down) {
int c = dataGridView1.CurrentCell.ColumnIndex;
int r = dataGridView1.CurrentCell.RowIndex;
if (r < dataGridView1.Rows.Count-1) //check for index out of range
dataGridView1.CurrentCell = dataGridView1[c, r + 1];
}
if (e.KeyCode == Keys.Up) {
int c = dataGridView1.CurrentCell.ColumnIndex;
int r = dataGridView1.CurrentCell.RowIndex;
if(r > 0) //check for index out of range
dataGridView1.CurrentCell = dataGridView1[c, r - 1];
}
}
}
提前抱歉。我不清楚你想要做什么。所以我对数据网格进行了genaric搜索,可能不是你想要的。
样本(这将搜索(循环)数据网格中的所有单元格返回第一个匹配项):
Sorry in advance. I'm not clear on what you are trying to do. So I made a genaric search for the datagrid, and may not be what you are looking for.
Sample (this will search (loop) all cells in the datagrid and return the first match):
private void TextBox_KeyDown(object sender, KeyEventArgs e) {
if (sender == textBox1) { //TextBox
//Search for... on Enter key,
//this can be any key allowed by the textbox
if (e.KeyCode == Keys.Enter) {
//check for no search data
if (textBox1.Text != string.Empty || textBox1.Text != "") {
//get search for data
DataGridCell cell = SearchRecords(textBox1.Text);
//check for valid cell, Not (-1,-1)
if(cell.ColumnNumber !=-1 && cell.RowNumber!= -1)
FocusOnEntry(cell);
}
}
}
}
private DataGridCell SearchRecords(object data) {
//Searches All Entries & Returns first matching
//Get Counts for validation
int c = dataGridView1.Columns.Count;
int r = dataGridView1.Rows.Count;
if (c != 0 && r != 0) { //Check if has Columns & Rows
//Search Columns Left > Right & Rows Top to Bottom
for (int col = 0; col < c; col++) {
for (int row = 0; row < r; row++) {
//Check Values for Match
DataGridViewCell cell = dataGridView1[col, row];
if (cell.Value != null && cell.Value.ToString() == data.ToString())
return new DataGridCell(row, col); //found
}
}
}
return new DataGridCell(-1, -1); //not found
}
private void FocusOnEntry(DataGridCell index) {
//Select the Row & Focus
dataGridView1.Rows[index.RowNumber].Selected = true;
dataGridView1.Focus();
}
如果您正在寻找,请告诉我。
Let me know if this is what you are looking for.
这篇关于我们可以在网格视图中上下记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文