Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView will not BeginEdit after BindingSource.Filter change
 

DataGridView will not BeginEdit after BindingSource.Filter change

I have a DataGridView that works normally until I change it's DataSource's BindingSource.Filter. After which it refuses to enter the edit mode.

DataGridView.BeginEdit(true); has no effect.

DataRow BeginEdit() has no effect either.

I've been working on this for many hours. Can someone please tell me how to get the DataGridView to BeginEdit?

Thanks,

Dennis
NDennisV  Sunday, December 02, 2007 3:02 PM

Hi, DennisV,

I can understand your problem now, and I think it is related to how you rebind a DataSource to your DataGridView.

As a matter of fact, everytime you rebind a new DataSource to the DataGridView, you shouldset it to null first.

Because before you doing that, the previous DataSource has already set the data structure and the values for the DataGridView, if you set a new DataSource, the data structure and values are not cleared, and DataGridView cannot find the proper value to show in the DataGridView. But setting the DataSource to null will clear them all.

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, December 07, 2007 2:37 AM

Hi, DennisV,

I cannot reproduce this issue.

No matter how I change the Filter property, I still can enter Edit mode.

Could you please post your codes here?

Regards

Yu Guo â€?MSFT  Thursday, December 06, 2007 3:20 AM
This is all of it in MakeNewFlashGrid(sender);


PrintPreviousdataGridView.Columns.Clear();
PrintPreviousdataGridView.AutoGenerateColumns = true;

tblSteamStrippingBindingSource.Filter = "Batch_Number = '" + BatchNumtextBox.Text + "'";
PrintPreviousdataGridView.DataSource = tblSteamStrippingBindingSource;

PrintPreviousdataGridView.Columns[0].Visible = false;
PrintPreviousdataGridView.Columns[1].Visible = false;
PrintPreviousdataGridView.Columns[3].Visible = false;
PrintPreviousdataGridView.Columns[4].Visible = false;
PrintPreviousdataGridView.Columns["Date_Time"].Width = 152;
PrintPreviousdataGridView.Columns["Flash"].Width = 51;
PrintPreviousdataGridView.Columns["Flash_Change_Per_Hr"].Width = 51;
PrintPreviousdataGridView.Columns["Flash_Change_Per_Hr"].HeaderText = "Hr_Change";
PrintPreviousdataGridView.Columns["Flash_Change_Per_Hr"].ToolTipText = "Flash Degree Change Per Hour";

PrintPreviousdataGridView.AllowUserToAddRows = false;
PrintPreviousdataGridView.AllowUserToDeleteRows = true;
PrintPreviousdataGridView.ReadOnly = false;


I have been able to make it work by putting these two lines just before the above code:


PrintPreviousdataGridView.DataSource = null;
PrintPreviousdataGridView.ReadOnly = true;



This DataGridView currently serves three different purposses. I noticed that when I went to another purpose, then returned to this purpose, the gird would enter into the edit mode. I narrowed it down to the two lines above.

The DataGridView's properties has ReadOnly set to true, and Visible = false;

Here is the code I use to call MakeNewFlashGrid(sender);


tblSteamStrippingBindingSource.Filter = null; // make sure there is data
if (tblSteamStrippingBindingSource.Find("Batch_Number", BatchNumtextBox.Text) == -1)
{
MessageBox.Show("No Data - Press Above Button First", " Steam Stripping");
return;
}
PrintPreviousdataGridView.Visible = true;
MakeNewFlashGrid(sender);


I have it working but I am curious as to why I could not make it work. And perhaps someone else will benifit from this post.

Dennis


NDennisV  Thursday, December 06, 2007 1:36 PM

Hi, DennisV,

I can understand your problem now, and I think it is related to how you rebind a DataSource to your DataGridView.

As a matter of fact, everytime you rebind a new DataSource to the DataGridView, you shouldset it to null first.

Because before you doing that, the previous DataSource has already set the data structure and the values for the DataGridView, if you set a new DataSource, the data structure and values are not cleared, and DataGridView cannot find the proper value to show in the DataGridView. But setting the DataSource to null will clear them all.

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, December 07, 2007 2:37 AM
Yes, but I also need to set ReadOnly to true and then back to false. I noticed that if I put ReadOnly = true then the next line ReadOnly = false, it still did not work. ReadOnly = false needs to be one of the last lines.

I found a doc about DataGridView on the Internet and it starts by saying "this is not an in-debth article" and it is 69 pages long!

I wonder if the DataGridView has gotten away from the developers?

Thanks for helping,

Dennis
NDennisV  Friday, December 07, 2007 3:24 AM

Hi, DennisV,

I did not meet the same issue in my test project. Maybe you can provide a more complete sample which can reproduce it.

DataGridView is really a complex but useful control. It is introduced in .Net Framework 2.0 to take the place of DataGrid control. If you use it regularly in your projects, it worth your time to learn it in depth. I am not sure who wrote this 69 pages document. But I think you should first learn some more about DataBinding model in .Net 2.0+.

http://support.microsoft.com/kb/313482

http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx

http://msdn2.microsoft.com/en-us/library/ms752347.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, December 07, 2007 3:39 AM

I had the same problem again in a different program, only this time the grid is very simple and serves only one purpose.

Anew grid row is created and filled from other textboxes after pressing a button, but the grid can alsobe edited.

I'm setting the grids BindingSource filterto a nonsense value when an empty form is first created/started (this clears the grid of all data).

Then after I create a new unique "batch_number" I set the grids BindingSource filter to that "batch_number."

But at this point I cannot directly edit the grid for some unknown reason. This is how I get it into the edit mode:

private void FlashPointsdataGridView_Enter(object sender, EventArgs e)

{

FlashPointsdataGridView.ReadOnly = true;

FlashPointsdataGridView.ReadOnly = false;

FlashPointsdataGridView.BeginEdit(true);

}

Just an FYI in case others have the same problem.

Dennis

NDennisV  Thursday, April 10, 2008 3:31 PM

You can use google to search for other answers

Custom Search

More Threads

• Problem setting new row cell a value...
• ADO.NET / Dynamic Controls Question - searching DataTable for Data
• asp.net + vb.net; error index out of range
• Datagridview : Select Multiple rows and process 1st cell
• How to set BindingSource of a detail BindingNavigator in a master-detail relation ?
• Browse Form - Edit Form
• building a dataset choosing a database as datasource
• DataGridView Performance issues with CellFormatting
• DatagridView in virtual mode(Unbound checkbox Column)unable to obtain dictionary key from dictionary
• Custom Business Object and DataGrid