0

For the life of me I cannot seem to figure this out. I have a long DataGridView (that does not allow MultiSelect) and when a user commits a change to the data, the data from the grid is purged and redrawn (because changes can affect multiple rows, this was the simpler approach). However, when I try to select the row programmatically, it does not also fire the DataGridView.SelectionChanged event, which I use to display data from an array which is correlated to the DataGridView current cell index. When doMagicStuff executes, the values for the wrong index (specifically, index 0) is show.

private void doMagicStuff()
{
    int selRow = myDGV.CurrentCell.RowIndex;
    myDGV.Rows.Clear();

    /*Perform Task, Redraw data*/
    myDGV.CurrentCell = myDGV[selRow, 0];
}
private void myDGV_SelectionChanged(object sender, EventArgs e)
{
    Label1.Text = myDisplayValue1[myDGV.CurrentCell.RowIndex];
    Label2.Text = myDisplayValue2[myDGV.CurrentCell.RowIndex];
    TextBox1.Text = myEditValue1[myDGV.CurrentCell.RowIndex];
    TextBox2.Text = myEditValue2[myDGV.CurrentCell.RowIndex];
}
Dan
  • 101
  • 1
  • 15
  • you can call the SelectionChanged event directly from your code – Gabriel GM Sep 21 '16 at 18:08
  • There are plenty of ways to get the currently selected row from the datagridview for starters you will need to use a `foreach (DataGridViewRow row in dgvReps.SelectedRows)` then from there you can set the value of what `row.Cells[cell Index you want].Value.ToString();` do a google search there are literally tons of working examples out there – MethodMan Sep 21 '16 at 18:08
  • @MethodMan the issue isn't that I don't know which row I've selected, the issue is that when I re-select my row the SelectionChanged event isn't firing, causing erroneous data to display in various fields on my form. – Dan Sep 21 '16 at 18:25
  • it's hard to tell when you don't show the code for that Event Code.. in the Designer .. make sure you have the Event wired up in the properties/Event section when you click on the DataGridView and it should be on the `SelectionChanged` Event there is also a `RowEnter` you can also add a button click to test what row you have selected.. – MethodMan Sep 21 '16 at 18:29
  • @MethodMan I updated the post to show the data in the current SelectionChanged event along with some additional error information. – Dan Sep 21 '16 at 18:36
  • did you put breakpoints in the event to truly make sure it's being hit..? also your code in the event is not correct in regards to getting the selected values – MethodMan Sep 21 '16 at 18:38
  • @Dan this is the easiest check. Put a break point on Label1.Text... When you run and click does it hit this break point? – FirebladeDan Sep 21 '16 at 18:38
  • @FirebladeDan No, that's why I'm posting here. When the row is changed at the end of doMagicStuff, it does not call the SelectionChanged event. – Dan Sep 21 '16 at 18:41
  • Can you provide your aspx and aspx.cs? I want to see your config and where what sits on client/server side. Sounds like your wiring is funky. – FirebladeDan Sep 21 '16 at 18:51
  • Just from the info you've provided a rebind is what you'd want to do. In doMagicStuff() http://stackoverflow.com/questions/7008361/how-can-i-refresh-c-sharp-datagridview-after-update – FirebladeDan Sep 21 '16 at 18:52
  • @FirebladeDan I'm not using ASP or DataSource values. I'm adding rows directly to the DataGridView object itself. 'private void fillRows(DataGridView dgv)' 'foreach(string s in myDisplayValue1)' 'dgv.Rows.Add(new object[] {s});' – Dan Sep 21 '16 at 18:54
  • I will post you something that you can try ..add a button to your form and double click the button and then I will post code that you can drop in that event to test.. select a grid row then click on the button and step through the code that I paste for you.. also you are doing this totally wrong to begin with.. – MethodMan Sep 21 '16 at 19:05

2 Answers2

0

Make sure that your client settings and OnSelectedIndexChanged is set like so: (ASP.NET AJAX)

.aspx page

<telerik:RadGrid ID="Grid1" runat="server" OnSelectedIndexChanged="Grid1_SelectedIndexChanged" OnItemDataBound="Grid1_ItemDataBound" OnPreRender="Grid1_PreRender">
             <ClientSettings EnablePostBackOnRowClick="true">
                      <Selecting AllowRowSelect="true"></Selecting>
             </ClientSettings>
</telerik:RadGrid>


aspx.cs page

protected void Grid1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string value = null;
        foreach(GridDataItem item in Grid1.SelectedItems)
        {
            //column name is in doub quotes
            value = item["Name"].Text;
        }
    }
FirebladeDan
  • 1,069
  • 6
  • 14
0

Add a button click to the form to test the selected values in the DataGridView.. double click that button then paste this code in there

foreach (DataGridViewRow row in myDGV.SelectedRows)
{
    Label1.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values 
    Label2.Text = //This should be hard coded the only thing that should change dynamically is the TextBox Values 
    TextBox1.Text = row.Cells[0].Value.ToString();//change the 0 or 1 to fit your column Index position 
    TextBox2.Text = row.Cells[2].Value.ToString();  
}

also if you have 4 columns and 4 text boxes then you will assign all of the textbox.Text values within the foreach loop just follow the pattern and increase the index by 1 so 2 textboxes means row.Cells[0] is the first column row.Cells[1] is the second column ...etc

MethodMan
  • 18,625
  • 6
  • 34
  • 52