Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

2nd Times change comboBox of dataGridview throw null reference exception

$
0
0

I have a dataGridView with a comboBox. The dataGridView binding data from dataTable with multiple coulmns and rows.

I set ValueMember of ComboBox as "Driver Id" and DisplayMember of the ComboBox as "Driver Name".Each row of the table,Value of Driver ID and Driver Name could be repeated as example.

In order to make sure the selection of comboBox no duplicate, I make use of another dataTable with filter duplicate then  set to dataSource of the comboxBox


private void buttonGo_Click(object sender, EventArgs e){

DataTable dt = new DataTable("Schedule"); dt.Columns.Add(new DataColumn("Driver Id",typeof(string))); dt.Columns.Add(new DataColumn("Driver Name", typeof(string))); for(int i; i <certainList.Count; i++) { CertainClass sch = (CertainClass)certainList[i]; dt.Rows.Add(); dt.Rows[i][0] = sch.DriverId+""; dt.Rows[i][1] = sch.DriverName; } DataGridViewSchedule.DataSource = dt; DataTable filterDt = new DataTable("Schedule"); filterDt.Columns.Add("Driver Id"); filterDt.Columns.Add("Driver Name"); for(int i; i <certainList.Count; i++) { ....////....if no duplicate then allow to add rows.... filterDt.Rows.Add(); filterDt.Rows[i][0] = sch.DriverId+""; filterDt.Rows[i][1] = sch.DriverName; } ComboBox.DataSource = filterDt; ComboBox.DataPropertyName = "Driver Id"; ComboBox.DisplayMember = "Driver Name"; ComboBox.ValueMember = "Driver Id"; ComboBox.ValueType=typeof(string);

}

Once change selection index of comboBox,first change it works fine,however it throw null reference exception if try to change comboxBox 2nd times.

private void dataGridViewSchedule_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox Column1 = (ComboBox)e.Control;
Column1.SelectedIndexChanged += new EventHandler(editingComboBox_SelectedIndexChanged);

}


private void editingComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
       ComboBox comboBox = (ComboBox)sender;

       if (comboBox != null)
       {
         //comboBox.SelectedIndexChanged += new EventHandler(comboBoxSelectedIndexChanged);
           object selected = comboBox.SelectedValue;
             ....////....
        }
     }catch(Exception){....}

}


System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridViewComboBoxCell.InitializeEditingControl(Int32 rowIndex, Object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
       at System.Windows.Forms.DataGridView.InitializeEditingControlValue(DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewCell dataGridViewCell)
       at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
       at System.Windows.Forms.DataGridViewComboBoxCell.OnMouseClick(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnCellMouseClick(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseClick(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.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 Ez.Newsletter.MagentoApi.Samples.Main(String[] args) in c:\Users\User\Documents\Visual Studio 2012\Projects\Ez.Newsletter.MagentoApi_v1.1\MagentoApi.Samples\Program.cs:line 83
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

I have refer follow link,but it doesn't help

http://social.msdn.microsoft.com/Forums/windows/en-US/5379430b-03b7-46be-a367-3fef9f857ef0/nullreferenceexception-after-selectedindexchanged-in-datagridviewcombobox?forum=winformsdatacontrols

--------

2nd Question,at the end of the day ,I would like to take out Driver Id and Driver Name columns from table,because comboBox already display same meaning, however valueMember and displayMember of the columns refer to Driver Id and Driver Name. I likely can't simply take out

Hope someone advice

Thanks



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>