Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView put on a panel in a form at design time.
A comboBox elsewhere on that form has code:-
private void comboBoxAdmin1_SelectedIndexChanged(object sender, EventArgs e)
   {
       Cursor.Current = Cursors.WaitCursor;
       switch (comboBoxAdmin1.SelectedItem)
       {
           case "People":
               dataGridView1.DataSource = Variables.ProjexDS.Tables["People"];
               break;
           case "Jobs":
               dataGridView1.DataSource = Variables.ProjexDS.Tables["Jobs"];
               break;
           case "Companies":
               dataGridView1.DataSource = Variables.ProjexDS.Tables["Companies"];
               break;
           default:
               Cursor.Current = Cursors.Default;
               return;
       }
       Cursor.Current = Cursors.Default;
       dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
   }

When I run the app and select - say - "Jobs" I get the DataTable Jobs correctly.
I've tried clicking, double clicking, F2 and other options but can't find any way to edit any cell in the DataGridView.
There must be something simple I am missing, can anyone see it?

What I have tried:

Every sort of click.
Searched for a long time, found lots of examples where the editing etc is achieved by code but not just using the DataGridView.
Posted
Updated 29-Dec-21 16:44pm

1 solution

Try this:

1) in the comboBoxAdmin1_SelectedIndexChanged handler check to make sure the user has not chosen the current data source.

2) try this

dataGridView1.SuspendLayout();

dataGridView1.DataSource = null;

dataGridView1.AutoGenerateColumns = true;

dataGridView1.Columns.Clear();

// your switch statement
dataGridView1.DataSource = user selected source
// end your switch statement

dataGridView1.ResumeLayout();
datagridView1.Refresh();

3) consider using a BindingSource: [^]

4) or using a BindingList: [^]
   
Comments
ormonds 30-Dec-21 0:10am
   
Thanks - that gives me a lot more detail about what to look for. I have tried your first suggestion and also the bindingsource solution but still can't edit the DataGridView.
ormonds 1-Jan-22 3:11am
   
Tried all those suggestions. Helpful when populating a DataGridView but don't help my problem which is that I can't edit the DataGridView.
BillWoodruff 2-Jan-22 11:58am
   
I wish I could assist further, but, without using your actual source code, I can only think of one more thing to check:

Set break-points after the DataGridView loads, and after you have reset the binding source:

When you hit the break-points: examine the 'Enabled and 'EditMode properties of the DataGridView: see any difference ?
ormonds 2-Jan-22 16:07pm
   
Thanks; yes, they are both true.
The method code is now:
private void comboBoxAdmin1_SelectedIndexChanged(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.Columns.Clear();
bindingSource1.DataSource = null;
Refresh();
switch (comboBoxAdmin1.Text)
{
case "People":
bindingSource1.DataSource = Variables.ProjexDS.Tables["People"];
break;
case "Jobs":
bindingSource1.DataSource = Variables.ProjexDS.Tables["Jobs"];
break;
case "Companies":
bindingSource1.DataSource = Variables.ProjexDS.Tables["Companies"];
break;
case "EmployeeClasses":
bindingSource1.DataSource = Variables.ProjexDS.Tables["EmployeeClasses"];
break;
case "ProfitCentres":
bindingSource1.DataSource = Variables.ProjexDS.Tables["ProfitCentres"];
break;
case "TimesheetActivities":
bindingSource1.DataSource = Variables.ProjexDS.Tables["TimesheetActivities"];
break;
case "TimesheetGroups":
bindingSource1.DataSource = Variables.ProjexDS.Tables["TimesheetGroups"];
break;
case "Activities":
bindingSource1.DataSource = Variables.ProjexDS.Tables["Activities"];
break;
case "MenuSecurity":
bindingSource1.DataSource = Variables.ProjexDS.Tables["MenuSecurity"];
break;
default:
Cursor.Current = Cursors.Default;
return;
}
dataGridView1.DataSource = bindingSource1;
dataGridView1.Refresh();
Cursor.Current = Cursors.Default;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
Refresh();
}
A typical table is populated so:-
private void populate_PeopleTable()
{
using (StreamWriter sw = File.AppendText("D:\\Projex.log"))
{
sw.WriteLine("Populating People Table");
sw.Close();
}
using (SqlConnection con40 = new SqlConnection(WorkbenchConnection))
{
con40.Open();
Variables.ProjexDA = new SqlDataAdapter();
LoadingState = "Setting up table People\r\n";
label26.Text += LoadingState;
Refresh();
Variables.ProjexDA.SelectCommand = new SqlCommand("SELECT * FROM People ORDER BY FullName", con40);
Variables.ProjexDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
Variables.ProjexDA.Fill(Variables.ProjexDS, "People");
Variables.PeopleTable = Variables.ProjexDS.Tables["People"];
Variables.PeopleTable.DefaultView.Sort = "FullName";
Variables.PeopleTable = Variables.PeopleTable.DefaultView.ToTable();
con40.Close();
}
}
In debug mode I've checked the value of:-
dataGridView1.DataSource is System.Windows.Forms.BingdingSource
Varianles.ProjexDS.Tables["Activities"].DefaultViewAllow Edit is true
Variables.ProjexDS.Tables["Activities"].DefaultViewAllowNew is true
dataGridView1.Columns.IsReadOnly is false
dataGridView1.DataBindings.IsReadOnly is false
Variables.ProjexDS.Tables["Activities"].Rows.IsReadOnly is false
Variables.ProjexDS.Tables["Activities"].Columns.IsReadOnly is false
dataGridView1.Enabled=true
dataGridView1.EditMode is EditOnKeyStrokeOrF2
Thanks for your help; I think my next step is to create a new project with one DataTable and one dataGridView.
BillWoodruff 2-Jan-22 23:43pm
   
well, i should not have mentioned "actual code" ... no time to try and decipher other people's complex code ... i'd be surprised if any of us who volunteer here would do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900