English 中文(简体)
更新数据后维持数据GridView 控件的选定行
原标题:Maintaining Selected Row of the DataGridView Control after refreshing Data
  • 时间:2012-05-23 04:08:25
  •  标签:
  • c#
最佳回答

您试过调试它吗? 我无法尝试它, 因为我不知道您何时会给 FillData 打电话, 除了表格的加载事件之外, 但我不认为这是您有问题的点。 我怀疑问题在于您总是跳过数据GridView1_ DataBinding Confollowlete 的选择部分, 因为 gSselect Plant 总是空的或者设置为第一行 。

发生这种情况通常是因为选择变更的解雇次数比您想象的要多许多倍, 特别是它被调用在 DataBinding Compllete 之前。 这意味着当您调用 FillData 时, 您应该“ 指示” 您的表格忽略选择变更的事件, 直到 DataBinding Compllete 被执行。 这可以做这样的拼写 :

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-088e88ec5af77/][1]

问题回答
//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;
                }           
            }

您是否检查了绑定的源1 。 find( “ PlantId ”, gSselectivePlant ) 是否返回正确的行索引?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签