?
Solved

Datagridview with combobox not working when trying to add a new row

Posted on 2011-10-07
5
Medium Priority
?
518 Views
Last Modified: 2012-05-12
I have a datagridview control, with a comboxboxcolumn. The datagridview control is populated using Linq to SQL, and the comboboxcolumn is populated using Linq to SQL as well. Everything works fine when I am editing an existing row's comboboxcolumn, but when I try and add a new row in the datagridview control, I get the following error message.

Here's all the code I use to populate my datagridview control, as well as my comboboxcolumn.
private void btnTimeGetTime_Click(object sender, EventArgs e)
        {
            try
            {
                OperationsDataContext opDC = new OperationsDataContext();
                dgTime.DataSource = null;

                dgTime.DataSource = opDC.ProjectHours_GetByProjectIDAndWeekEndingDate(Convert.ToInt32(cboTimeProject.SelectedValue), dtWeekEnding.Value);

                AddComboBoxColumns();

                dgTime.Columns["ProjectHourID"].ReadOnly = true;
                dgTime.Columns["WeekEndingDate"].ReadOnly = true;
                dgTime.Columns["TaskID"].ReadOnly = true;

                dgTime.AutoResizeColumns();
            }
            catch (Exception ex)
            {
            }
        }

private void AddComboBoxColumns()
        {
            DataGridViewComboBoxColumn comboboxColumn;
            comboboxColumn = CreateComboBoxColumn();
            SetAlternateChoicesUsingDataSource(comboboxColumn);
            comboboxColumn.HeaderText = "Task";
            dgTime.Columns.Insert(2, comboboxColumn);
        }

        private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
        {
            OperationsDataContext dc = new OperationsDataContext();
            {
                comboboxColumn.DataSource = dc.Tasks_GetByProjectID(Convert.ToInt32(cboTimeProject.SelectedValue));
                comboboxColumn.ValueMember = "TaskID";
                comboboxColumn.DisplayMember = "TaskName";
            }
        }

        private DataGridViewComboBoxColumn CreateComboBoxColumn()
        {
            DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
            {
                column.DataPropertyName = "TaskID";
                column.HeaderText = "TaskName";
                column.DropDownWidth = 160;
                column.Width = 90;
                column.MaxDropDownItems = 3;
                column.FlatStyle = FlatStyle.Flat;
            }
            return column;
        }

Open in new window

DatagridviewWithComboboxColumnNo.png
DatagridviewWithComboboxColumnWo.png
0
Comment
Question by:jjliu4492
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:Friman001
ID: 36932545
To me, it looks like you need to have a value in the combobox for it to like it.  In other words, maybe see if you can have it accept null values instead or maybe you just need to put a 0 in the "tasks" column or select a value you from it before you can create it.  I just think it looks like it is not excepting the value you are starting with to create the new row.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36932568
You could also try setting the first value to "" or 0.  It really looks like being able to accept null values from the first picture though.  I am just throwing out some idea.  Please write back and I will try to help point you in the right direction if I can!
0
 

Accepted Solution

by:
jjliu4492 earned 0 total points
ID: 36932572
-Open the DataGrid properties
Click in the DataError event line
-Type _DoNothingOnDataError

        private void _DoNothingOnDataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
0
 

Author Closing Comment

by:jjliu4492
ID: 36954292
I found the answer on google.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36932586
Cool!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question