Puca

Hi, I'm using vs21005, .net 2 for windows application. I'm doing drag and drop between 2 datagridview controls. When the drop is completed, I would like to highlight the row that I copy the inofomation to in the target grid. The prolem is my grid has a sort column and once the information is copied to the target row, the target grid sorts itself with the new information and the row highlighted in the grid is no longer row that has the copied information becuase it's been moved to new rows number in the grid. How can I have the correct row highlighted Should I turn off the sort on the gird before I update the grid row How can I turn off the sort Thanks.


Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

boban.s

Create a handler for RowAdded event and execute:
myDataGrid.Rows[e.RowIndex].Selected = true;




Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Puca

Thank you for the reply. I tried the code and it has the same problem as if I change the row color myself. The row that is copied with the source row information gets reposition after it got the new data. Its old row position then gets highlighted. For example, if it's row #2 that got new data, Row #2 gets repositioned to row #11 after it got new data. Whatever is now position as the row #2 is now highlighted.






Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Zhi-Xin Ye - MSFT

You can use a Tag to note the dragged row, set it to be selected. Something like this:

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

private void Form2_Load(object sender, EventArgs e)

{

this.dataGridView1.Columns.Add("c1","c1");

DataGridViewRow dr = new DataGridViewRow();

dr.CreateCells(this.dataGridView1,"aa");

this.dataGridView1.Rows.Add(dr);

DataGridViewRow dr1 = new DataGridViewRow();

dr1.CreateCells(this.dataGridView1, "cc");

dr1.Tag = "dragged";

this.dataGridView1.Rows.Add(dr1);

DataGridViewRow dr2 = new DataGridViewRow();

dr2.CreateCells(this.dataGridView1, "bb");

this.dataGridView1.Rows.Add(dr2);

this.dataGridView1[0, 0].Selected = false;

}

private void button1_Click(object sender, EventArgs e)

{

foreach (DataGridViewRow row in this.dataGridView1.Rows)

{

if (row.Tag != null && row.Tag.ToString() == "dragged")

{

row.Selected = true;

}

}

}

}






Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Puca

Thank you Ye. I already tried something similar to your example. The problem is that datagrid re-sort itself after I copied the data from the source grid row to the target grid row(I'm not creating new rows at the target grid). The grid resort itself and the wrong row on the target grid is highlighted. Do you know in which event should I hight light the datarow Thanks.




Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Zhi-Xin Ye - MSFT

Have you tried the Sorted event




Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Puca

Yes, I already tried the Sorted event and that is raised. The hilighting code I put in the DoDragDrop event highlights the row before it resorts it self so it hightlights the wrong row. Thanks.




Re: Windows Forms Data Controls and Databinding Need help hilighting the correct row on the datagridview control.

Puca

Sorry, there was a typo in my previous reply. I meant to say " the Sorted event IS NOT raised. ". If the Sorted event is raised then I would have put the highlited code in there. Thanks.