C# 基于列和值在dataGridView中查找行
我有一个dataGridView,它有3列:SystemId、FirstName、LastName,它们是使用数据库信息绑定的。我想突出显示某一行,我将使用:C# 基于列和值在dataGridView中查找行,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个dataGridView,它有3列:SystemId、FirstName、LastName,它们是使用数据库信息绑定的。我想突出显示某一行,我将使用: dataGridView1.Rows[????].Selected = true; 但是,我不知道行ID,bindingsource不断更改,因此,第10行在一个实例中可能是“John Smith”,但在另一个实例中甚至不存在(我有一个过滤器,它根据用户输入的内容过滤掉源代码,因此键入“joh”将生成名/姓为“joh”的所有行)因此,我
dataGridView1.Rows[????].Selected = true;
但是,我不知道行ID,bindingsource不断更改,因此,第10行在一个实例中可能是“John Smith”,但在另一个实例中甚至不存在(我有一个过滤器,它根据用户输入的内容过滤掉源代码,因此键入“joh”将生成名/姓为“joh”的所有行)因此,我的列表可以在一次单击中从50个名称变为3个名称)
我想找到一种基于SystemId和相应数字选择行的方法。我可以使用以下方法获取系统ID:
systemId = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SystemId"].Value.ToString();
现在我只需要将它应用于行选择器。类似于dataGridView1.Columns[“SystemId”].IndexOf(SystemId}但这不起作用(也不存在这种方法)。非常感谢您的帮助。这将为您提供值的gridview行索引:
String searchValue = "somestring";
int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
if(row.Cells[1].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
break;
}
}
或者LINQ查询
int rowIndex = -1;
DataGridViewRow row = dgv.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue))
.First();
rowIndex = row.Index;
如果您只想检查该项目是否存在:
IEnumerable<DataGridViewRow> rows = grdPdfs.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue));
if (rows.Count() == 0)
{
// Not Found
}
else
{
// Found
}
IEnumerable rows=grdPdfs.rows
.Cast()
其中(r=>r.Cells[“SystemId”].Value.ToString().Equals(searchValue));
if(rows.Count()==0)
{
//找不到
}
其他的
{
//发现
}
或者您可以这样使用。这可能会更快
int iFindNo = 14;
int j = dataGridView1.Rows.Count-1;
int iRowIndex = -1;
for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count/2) +1; i++)
{
if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == iFindNo)
{
iRowIndex = i;
break;
}
if (Convert.ToInt32(dataGridView1.Rows[j].Cells[0].Value) == iFindNo)
{
iRowIndex = j;
break;
}
j--;
}
if (iRowIndex != -1)
MessageBox.Show("Index is " + iRowIndex.ToString());
else
MessageBox.Show("Index not found." );
int-iFindNo=14;
int j=dataGridView1.Rows.Count-1;
int-iRowIndex=-1;
对于(inti=0;i
试试这个:
string searchValue = textBox3.Text;
int rowIndex = -1;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["peseneli"].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
break;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
只有当
allowUserToAddress
设置为false
时,上述答案才有效。如果该属性设置为true
,那么当循环或Linq查询尝试协商新行时,您将得到NullReferenceException
。我已修改了上面两个接受的答案来处理allowUserToAddress=true
循环回答:
String searchValue = "somestring";
int rowIndex = -1;
foreach(DataGridViewRow row in DataGridView1.Rows)
{
if (row.Cells["SystemId"].Value != null) // Need to check for null if new row is exposed
{
if(row.Cells["SystemId"].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
break;
}
}
}
林奇回答:
int rowIndex = -1;
bool tempAllowUserToAddRows = dgv.AllowUserToAddRows;
dgv.AllowUserToAddRows = false; // Turn off or .Value below will throw null exception
DataGridViewRow row = dgv.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["SystemId"].Value.ToString().Equals(searchValue))
.First();
rowIndex = row.Index;
dgv.AllowUserToAddRows = tempAllowUserToAddRows;
introwindex=-1;
bool tempallowUserToAddress=dgv.allowUserToAddress;
dgv.AllowUserToAddress=false;//关闭或。下面的值将引发null异常
DataGridViewRow行=dgv.Rows
.Cast()
.Where(r=>r.Cells[“SystemId”].Value.ToString().Equals(searchValue))
.First();
rowIndex=行索引;
dgv.AllowUserToAddress=临时AllowUserToAddress;
这是基于戈登的上述答案——并非所有答案都是我的原创作品。
我所做的是向静态实用程序类添加一个更通用的方法
public static int MatchingRowIndex(DataGridView dgv, string columnName, string searchValue)
{
int rowIndex = -1;
bool tempAllowUserToAddRows = dgv.AllowUserToAddRows;
dgv.AllowUserToAddRows = false; // Turn off or .Value below will throw null exception
if (dgv.Rows.Count > 0 && dgv.Columns.Count > 0 && dgv.Columns[columnName] != null)
{
DataGridViewRow row = dgv.Rows
.Cast<DataGridViewRow>()
.FirstOrDefault(r => r.Cells[columnName].Value.ToString().Equals(searchValue));
rowIndex = row.Index;
}
dgv.AllowUserToAddRows = tempAllowUserToAddRows;
return rowIndex;
}
那些使用WPF的人
for (int i = 0; i < dataGridName.Items.Count; i++)
{
string cellValue= ((DataRowView)dataGridName.Items[i]).Row["columnName"].ToString();
if (cellValue.Equals("Search_string")) // check the search_string is present in the row of ColumnName
{
object item = dataGridName.Items[i];
dataGridName.SelectedItem = item; // selecting the row of dataGridName
dataGridName.ScrollIntoView(item);
break;
}
}
这真的节省了我很多时间,在我看来,最好的方法是linq方法:我会将DataBoundItem清晰地转换到ViewModel中,并直接对其属性进行比较……然后您就不需要依赖列名了。不过,我选择的解决方案,谢谢!感谢此解决方案-非常容易实现。我只做了更改was to do
DGV.Rows[row.Index].Selected=true;break;
保存第二行注释尝试:对我来说,上面的select语句没有任何移动。在别处可以找到(Alex Filipovici)dgvIcbSubsInfo.CurrentCell=dgvIcbSubsInfo.Rows[currentRow].Cells[0];如果您只想检查行的存在,请使用.Any()我已经晚了几年,但我认为迟做总比不做强。:)因为这个发现给你一个+1
private void UndeleteSectionInGrid(string sectionLetter)
{
int sectionRowIndex = UtilityMethods.MatchingRowIndex(dgvSections, "SectionLetter", sectionLetter);
dgvSections.Rows[sectionRowIndex].Cells["DeleteSection"].Value = false;
}
for (int i = 0; i < dataGridName.Items.Count; i++)
{
string cellValue= ((DataRowView)dataGridName.Items[i]).Row["columnName"].ToString();
if (cellValue.Equals("Search_string")) // check the search_string is present in the row of ColumnName
{
object item = dataGridName.Items[i];
dataGridName.SelectedItem = item; // selecting the row of dataGridName
dataGridName.ScrollIntoView(item);
break;
}
}
DataRowView drv = dataGridName.SelectedItem as DataRowView;
DataRow dr = drv.Row;
string item1= Convert.ToString(dr.ItemArray[0]);// get the first column value from selected row
string item2= Convert.ToString(dr.ItemArray[1]);// get the second column value from selected row