Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Unique checkboxcolumn restictions
 

Unique checkboxcolumn restictions

I have a databound datagridview with 3 columns as shown below. The last column is a checkbox column. I'm looking for a way to prevent the user from having a 1 of True value for the checkbox column (UnitSelectionFlag) for any two like values in the ProductUnitID column. Initially all the values in the checkboxcolumn are 0 or False (unchecked). As the user changes these to 1 or True I want them to be stopped when (for example) more then one of the rows with a value of 1 in the ProductID column has a value of 1 in the unitselectionflag column.

OrderRentalDetailIDProductUnitIDUnitSelectionFlag
1310
4310
1320
4320
1330
4330
1340
4340
1150
3050
MethodMas  Thursday, September 24, 2009 5:23 AM
You can use CellEndEdit event of datagridview and selectthe count of records matching to your unique criteriarecords from datasource.

Here is a sample code.

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                DataTable dt = ((DataTable)dataGridView1.DataSource);
                if (Convert.ToBoolean(dt.Rows[e.RowIndex][e.ColumnIndex]))
                {
                    int count = dt.Select("OrderSelectionFlag=1 and ProductUnitID=" + dt.Rows[e.RowIndex][1].ToString()).Length;
                    if (count > 1)
                    {
                        MessageBox.Show("Error: You can not select this");
                        dt.Rows[e.RowIndex][e.ColumnIndex] = false;
                    }
                }
            }
        }
Tamer Oz  Thursday, September 24, 2009 6:52 AM
You can use CellEndEdit event of datagridview and selectthe count of records matching to your unique criteriarecords from datasource.

Here is a sample code.

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                DataTable dt = ((DataTable)dataGridView1.DataSource);
                if (Convert.ToBoolean(dt.Rows[e.RowIndex][e.ColumnIndex]))
                {
                    int count = dt.Select("OrderSelectionFlag=1 and ProductUnitID=" + dt.Rows[e.RowIndex][1].ToString()).Length;
                    if (count > 1)
                    {
                        MessageBox.Show("Error: You can not select this");
                        dt.Rows[e.RowIndex][e.ColumnIndex] = false;
                    }
                }
            }
        }
Tamer Oz  Thursday, September 24, 2009 6:52 AM
I converted the code to vb.net, replaced datagridview1.datasource withcheckinoutunitdatagriveiw.datasource (the name of my datagridview)and tried to use it but I keep getting the exception that a bindingsource type cannot be covnerted to a datatable type. Since my datagridview is bound using the bindingsource, how would I correct this error?
MethodMas  Thursday, September 24, 2009 3:07 PM
try

((

BindingSource)dataGridView1.DataSource)[0]

Tamer Oz  Thursday, September 24, 2009 3:21 PM
It's still not working. Forgive me, I must be missing something simple. Here's what I coded:
    Private Sub CheckInOutUnitAddDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles CheckInOutUnitAddDataGridView.CellEndEdit
        If e.ColumnIndex = 2 Then
            Dim dt As DataTable = DirectCast(CheckInOutUnitAddDataGridView.DataSource, BindingSource)(0)
            If Convert.ToBoolean(dt.Rows(e.RowIndex)(e.ColumnIndex)) Then
                Dim count As Integer = dt.[Select]("OrderSelectionFlag=1 and ProductUnitID=" & dt.Rows(e.RowIndex)(1).ToString()).Length
                If count > 1 Then
                    MessageBox.Show("Error: You can not select this")
                    dt.Rows(e.RowIndex)(e.ColumnIndex) = False
                End If
            End If
        End If
    End Sub
MethodMas  Thursday, September 24, 2009 5:32 PM
I'm very close with this one. If you can help me get the coding correctly I'd be very appreciative. Thanks again for all of your help thus far.
MethodMas  Friday, September 25, 2009 6:09 AM
try

Dim dt As DataTable = DirectCast(DirectCast(dataGridView1.DataSource, BindingSource).DataSource, DataSet).Tables(dataGridView1.DataMember)
Tamer Oz  Friday, September 25, 2009 6:20 AM
I'm still getting

"Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataSet'." if I use:

Dim dt As DataTable = DirectCast(DirectCast(CheckInOutUnitAddDataGridView.DataSource, BindingSource).DataSource, DataSet).Tables(CheckInOutUnitAddDataGridView.DataMember)

MethodMas  Friday, September 25, 2009 3:44 PM
Whatis the type of object of your bindingsource's datasource?
Tamer Oz  Friday, September 25, 2009 3:50 PM
Does your UnitSelectionFlag field contains boolean data or integer data(0or 1)?
Tamer Oz  Friday, September 25, 2009 3:54 PM
The bindingsource is bound to a TableAdapter with the standard method used by the Visual Studio Wizards.

MethodMas  Friday, September 25, 2009 5:40 PM
the UnitSelectFlag contains boolean data but this particular column is not databound while the other columns are.
MethodMas  Friday, September 25, 2009 5:45 PM
I dont understand why it doesn't work.

I fooled.

Could you send the latest code inCellEndEdit event and detailed information on error
Tamer Oz  Friday, September 25, 2009 5:54 PM
    Private Sub CheckInOutUnitAddDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles CheckInOutUnitAddDataGridView.CellEndEdit
        If e.ColumnIndex = 2 Then
            Dim dt As DataTable = DirectCast(DirectCast(CheckInOutUnitAddDataGridView.DataSource, BindingSource).DataSource, DataSet).Tables(CheckInOutUnitAddDataGridView.DataMember) 
            If Convert.ToBoolean(dt.Rows(e.RowIndex)(e.ColumnIndex)) Then
                Dim count As Integer = dt.[Select]("OrderSelectionFlag=1 and ProductUnitID=" & dt.Rows(e.RowIndex)(1).ToString()).Length
                If count > 1 Then
                    MessageBox.Show("Error: You can not select this")
                    dt.Rows(e.RowIndex)(e.ColumnIndex) = False
                End If
            End If
        End If
    End Sub

Error Message:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataSet'."
Source="Transact"
StackTrace:
at Transact.CheckInOutForm.CheckInOutUnitAddDataGridView_CellEndEdit2(Object sender, DataGridViewCellEventArgs e) in C:\Path\CheckInOutForm.vb:line 247
at System.Windows.Forms.DataGridViewCellEventHandler.Invoke(Object sender, DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnCellContentClick(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnCommonCellContentClick(Int32 columnIndex, Int32 rowIndex, Boolean doubleClick)
at System.Windows.Forms.DataGridViewCell.OnMouseUpInternal(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnCellMouseUp(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Transact.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
MethodMas  Friday, September 25, 2009 6:49 PM

I can't see any error, and I can't find the problem.

Sorry.

Check thelink below, if your problem is not solved you can send thesource(only that form)to me then I'm sure Ican find the problem.

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/30ab63cb-f436-4ff5-85c8-4d25c5c55700

Tamer Oz  Friday, September 25, 2009 7:04 PM

You can use google to search for other answers

Custom Search

More Threads

• How to associate datagridview with list<>
• Binding Combobox.SelectedItem to Objects with NULL values
• Passing paremeters in SQL DataSource
• Inserting and updating rows..
• databinding to textbox problem
• How Do I switch a datasource on a combobox "on the fly"?
• Double CurrencyManager
• DateTime picker problems
• DataGridView EditingControlShowing Cells blackout
• Change BindingNavigator.Validate behaviour