Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you tried debugging it? I can't try it since I don't know when you call FillData, apart from the loading event of the forms, but I don't think it is the point where you have the problem. I suspect that the problem is that you always skip the selection part of dataGridView1_DataBindingComplete because gSelectedPlant is always empty or set to the first row. </p> <p>This usually happens because SelectionChanged is fired many more times than you think, in particular it is called before DataBindingComplete. This means that when you call FillData you should "instruct" your form to ignore the SelectionChanged events until the DataBindingComplete has been executed. This can be done modyfing your code something like this:</p> <pre><code>public partial class frmPlant : Form { string gSelectedPlant; bool ignoreSelChg = false; // &lt;- 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; // &lt;- 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) // &lt;- don't do anything before DataBindingComplete return; if (dataGridView1.SelectedRows.Count &gt; 0) { gSelectedPlant = dataGridView1.SelectedRows[0].Cells["PlantId"].Value.ToString(); } } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { int selectedIndex; if (!string.IsNullOrEmpty(gSelectedPlant) &amp;&amp; e.ListChangedType == ListChangedType.Reset) { ignoreSelChg = false; // &lt;- reset the bool, SelectionChanged get executed again if (ds.Tables.Count &gt; 0) { selectedIndex = bindingSource1.Find("PlantId", gSelectedPlant); if (selectedIndex &lt;= 0) selectedIndex = 0; dataGridView1.Rows[selectedIndex].Selected = true; } else { gSelectedPlant = string.Empty; } } } } </code></pre> <p>You can take a look at the posts of Mark Rideout here: <code>[http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/01f937af-d0d0-4de5-8919-088e88c5af77/][1]</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload