Binu

I have a data bound (Programmatically) DataGridView which is editable. One of the column is used as flag (invisible column), I want to make the rows readonly based on the value of this flag column. If I sort based on a column and changes the value of that column in a specific row, after the rows gets re arranged (because of the sorting), the datagrid rows are becoming readonly incorrectly due to sorting. I tried handling this in “Sorted” event but this event will fire only when we click on the column header to sort. Do you suggest how can I handle or maintain the row position after an already applied sort.



Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Gavin Jin - MSFT

Hi, binu

I think you can use DataGridView1_SortCompare sub to have a custom sort. In this event,you can set the row's positon and the readonly property. Hope it helps.

Regards.






Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Binu

Hi Gavin,

Thanks for the reply.

SortComapre event will fire only when we click the Grid header.

The issue happens not just after sorting by clicking the column header.

This happens after a column is already sorted, the user changes the data of the same column and the position of the row will be changed according to the changed value.

Thanks & Regards,

Binu





Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Zhi-Xin Ye - MSFT

I wonder how you set the ReadOnly property for the rows according to the value in flag column, but to handle the cellFormating event would work in despite of sorting. Have a look at my sample:

Code Snippet

public partial class Form1: Form

{

public Form1()

{

InitializeComponent();

}

DataTable dt;

private void Form1_Load(object sender, EventArgs e)

{

dt = new DataTable();

dt.Columns.Add("editable", typeof(bool));

dt.Columns.Add("value");

dt.Rows.Add(true, "aaa");

dt.Rows.Add(false, "abc");

dt.Rows.Add(true, "1ab");

dt.Rows.Add(true, "cda");

dt.Rows.Add(false, "cdt");

this.dataGridView1.DataSource = dt;

this.dataGridView1.Columns[0].Visible = false;

this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

}

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)

{

if ((bool)this.dataGridView1[0, e.RowIndex].Value == false)

this.dataGridView1.Rows[e.RowIndex].ReadOnly = true;

}

}

}






Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Binu

Hi Zhi-Xin Ye,

Thank you for the suggestion.

But this will not work. Please do the following:

1. Change the sort order (Change to descending) by clicking column header. ("1ab" will be the first value, "abc" and "cdt" are readonly -- this is correct).

2. Change the one of the value in a row so that the column gets re sorted automatically. (For e.g. Change the Value "cda" to "111")

You will see that the rows are becoming readonly incorrectly (now "aaa","abc" and "cdt" are readonly -- this is incorrect as "aaa" should be editable).

Can anyone, please suggest a way to solve this issue.

Thanks,

Binu





Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Binu

Hi,

Do you got any suggestion to solve this issue.

Thanks,

Binu





Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

timvw

If i experiment with a BindingList<T> that supports sorting i notice that every time the user sorts a ListChanged event is raised.. So perhaps you want to subscribe to that event.





Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Binu

Hi Tim,

I do not think this is doable as my grid binding is performed programatically.

Can you please explain

Thanks,

Binu





Re: Windows Forms Data Controls and Databinding Maintain the row position of datagridview after sorting.

Binu

As a workaround, I am planning to disable sorting on all the editable fields to avoid this issue.

But it is unfortunate that the DGV does not have an event to handle this, can any one suggest a way to report this issue to Microsoft

Thanks,

Binu