1

我试图在刷新数据后维护 DataGridView 控件的选定行。这是我的代码

 public partial class frmPlant : Form
    {
        string gSelectedPlant;

     private void frmPlant_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = bindingSource1;
            FillData();

            dataGridView1.DataMember = "Table";
}
 private void FillData()
        {
            ds = _DbConnection.returnDataSet(_SQlQueries.SQL_PlantSelect);
            bindingSource1.DataSource = ds.Tables[0];
        }
 public DataSet returnDataSet(string txtQuery)
        {
            conn.Open();
            sqlCommand = conn.CreateCommand();
            DB = new SQLiteDataAdapter(txtQuery, conn);
            DS.Reset();
            DB.Fill(DS);
            conn.Close();
            return (DS);
        }
  private void dataGridView1_Selectionchanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                gSelectedPlant = dataGridView1.SelectedRows[0].Cells["PlantId"].Value.ToString();
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            int selectedIndex;
            if (!string.IsNullOrEmpty(gSelectedPlant) && e.ListChangedType == ListChangedType.Reset)
            {
                if (ds.Tables.Count > 0)
                {
                    selectedIndex = bindingSource1.Find("PlantId", gSelectedPlant);
                    if (selectedIndex <= 0)
                        selectedIndex = 0;
                    dataGridView1.Rows[selectedIndex].Selected = true;
                }
                else
                {
                    gSelectedPlant = string.Empty;
                }
            }
        }
    }

它仍然无法维护所选行的行索引。它滚动到第 1 行。这是我使用的博客 http://www.makhaly.net/Blog/9

假设,我在 Form1(所有代码所在的位置)上选择一行,然后进入下一个表单,它向我显示了特定 Plant 的详细信息。如果我再次回到第一个表单,按下后退按钮,该行将重置为 1。gSelectedPlant 取值 1 和 selectedindex = 0。这是有道理的,但我还不能弄清楚如何保持该值gSelectedPlant 的。是的,它最初需要一个空值,但在数据绑定完成时它变为 1。

4

3 回答 3

0

你试过调试它吗?我无法尝试,因为除了表单的加载事件之外,我不知道您何时调用 FillData,但我认为这不是您遇到问题的关键。我怀疑问题在于您总是跳过 dataGridView1_DataBindingComplete 的选择部分,因为 gSelectedPlant 始终为空或设置为第一行。

这通常是因为 SelectionChanged 的​​触发次数比您想象的要多得多,尤其是在 DataBindingComplete 之前调用它。这意味着当您调用 FillData 时,您应该“指示”您的表单忽略 SelectionChanged 事件,直到 DataBindingComplete 已执行。这可以通过修改您的代码来完成,如下所示:

public partial class frmPlant : Form
{
     string gSelectedPlant;
     bool ignoreSelChg = false;  // <- added this bool    

     private void frmPlant_Load(object sender, EventArgs e)
     {
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = bindingSource1;
            FillData();

            dataGridView1.DataMember = "Table";
     }

     private void FillData()
     {
            ignoreSelChg = true; // <- set the bool, SelectionChanged won't do anything now

            ds = _DbConnection.returnDataSet(_SQlQueries.SQL_PlantSelect);
            bindingSource1.DataSource = ds.Tables[0];
     }

     public DataSet returnDataSet(string txtQuery)
     {
            conn.Open();
            sqlCommand = conn.CreateCommand();
            DB = new SQLiteDataAdapter(txtQuery, conn);
            DS.Reset();
            DB.Fill(DS);
            conn.Close();
            return (DS);
     }

     private void dataGridView1_Selectionchanged(object sender, EventArgs e)
     {
            if (ignoreSelChg)  // <- don't do anything before DataBindingComplete
                return;

            if (dataGridView1.SelectedRows.Count > 0)
            {
                gSelectedPlant = dataGridView1.SelectedRows[0].Cells["PlantId"].Value.ToString();
            }
     }

     private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            int selectedIndex;
            if (!string.IsNullOrEmpty(gSelectedPlant) && e.ListChangedType == ListChangedType.Reset)
            {
                ignoreSelChg = false; // <- reset the bool, SelectionChanged get executed again

                if (ds.Tables.Count > 0)
                {
                    selectedIndex = bindingSource1.Find("PlantId", gSelectedPlant);
                    if (selectedIndex <= 0)
                        selectedIndex = 0;
                    dataGridView1.Rows[selectedIndex].Selected = true;
                }
                else
                {
                    gSelectedPlant = string.Empty;
                }
            }
        }
    }

您可以在此处查看 Mark Rideout 的帖子:[http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/01f937af-d0d0-4de5-8919-088e88c5af77/][1]

于 2012-05-23T04:57:47.380 回答
0
//public area
 int selectedID,rowIndex, scrollIndex;
 bool IsSelectedRow;

 private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  {
      if (e.RowIndex < 0)
          return;
      selectedID = (int)DataGridView1.SelectedRows[0].Cells[0].Value;
      scrollIndex = DataGridView1.FirstDisplayedScrollingRowIndex;
      IsSelectedRow = true;
    }

    private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
            {
                if (IsSelectedRow)
                {
                    foreach (DataGridViewRow row in DataGridView1.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(selectedID.ToString()))
                        {
                            rowIndex = row.Index;
                            break;
                        }
                    }
                    DataGridView1.Rows[rowIndex].Selected = true;
                }           
            }
于 2013-11-03T09:49:57.420 回答
-1

你检查 bindingSource1.Find("PlantId", gSelectedPlant); 返回正确的行索引?

于 2012-05-23T04:53:12.773 回答