【问题标题】:Issues in Visual C# 2005 finding and selecting a Row in Dataview to be highlightVisual C# 2005 中的问题在 Dataview 中查找并选择要突出显示的行
【发布时间】:2012-05-18 14:22:18
【问题描述】:

我有一个数据表,里面填满了各种行,每行都有几列(即 ID、名称、描述..)

我正在尝试实现一个搜索按钮,其中用户通过组合框选择一列,例如 ID 或名称,并在单击搜索按钮后在文本框中键入他们请求的搜索我想要该行他们正在寻找在屏幕上选择和突出显示的内容,而不将表格作为关键,将所有内容保持在表格最初填充的顺序中。另一个注意事项是我希望所有项目仍然可见,所以我不打算使用 .rowfilter ,因为它已经在单独的按钮中实现。 我已经弄清楚如何通过它的索引定位该行并相应地更新了代码。只是想知道我如何在所述行上居中并突出显示它而不会混淆表格的顺序

这是我目前所拥有的:

private void buttonSearch_Click(object sender, EventArgs e)
{
    string query = textSearch.Text;
    int intquery = Int32.Parse(query);
    DataTable dt = GetAlarmTable();
    DataView dv = new DataView(dt);
    DataRow[] foundRows;
    switch (comboboxSearch.Text)
    {
         case "ID":
         {

              dv.Sort = "ID"; 
              index = dv.Find(intquery);
              foundRows = dt.Select("ID = '" + intquery + "'");
              break;
          } 
    }
//??? ???

}

如果您有要分享的代码、想法或链接,请告诉我!

【问题讨论】:

    标签: c# winforms datatable visual-studio-2005 dataview


    【解决方案1】:

    这是我通常使用的功能。确保将DataGridView 上的SelectionMode 属性设置为FullRowSelect

    public static void EnsureGridRowIsSelectedAndVisible(DataGridViewRow row)
    {
        if (row == null)
            return;
    
        row.Selected = true;
    
        // Center the item in the display
        row.DataGridView.FirstDisplayedScrollingRowIndex = 
    
            Math.Max(row.Index - Convert.ToInt32(row.DataGridView.DisplayedRowCount(false) / 2), 0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多