Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At least one of the DataGridView control's columns has no cell template

Tags:

I'm getting that exception.

System.InvalidOperationException was unhandled
  Message=At least one of the DataGridView control's columns has no cell template.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridView.CompleteCellsCollection(DataGridViewRow dataGridViewRow)
       at System.Windows.Forms.DataGridView.get_RowTemplateClone()
       at System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView)
       at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
       at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
       at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
       at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
       at System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value)
       at System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext value)
       at System.Windows.Forms.ContainerControl.get_BindingContext()
       at System.Windows.Forms.Control.get_BindingContextInternal()
       at System.Windows.Forms.Control.get_BindingContext()
       at System.Windows.Forms.DataGridView.DataGridViewDataConnection.SetDataConnection(Object dataSource, String dataMember)
       at System.Windows.Forms.DataGridView.set_DataSource(Object value)
       at Bloowars_Tools.A1_ResultWnd.A1_ResultWnd_Shown(Object sender, EventArgs e) in D:\Documents\Visual Studio 2010\Projects\Bloowars Tools\Bloowars Tools\A1_ResultWnd.cs:line 72
       at System.Windows.Forms.Form.OnShown(EventArgs e)
       at System.Windows.Forms.Form.CallShownEvent()
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.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(IntPtr 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.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at Bloowars_Tools.MainWnd.bt_A1_Wyniki_Click(Object sender, EventArgs e) in D:\Documents\Visual Studio 2010\Projects\Bloowars Tools\Bloowars Tools\Form1.cs:line 165
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.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(IntPtr 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(Form mainForm)
       at Bloowars_Tools.Program.Main() in D:\Documents\Visual Studio 2010\Projects\Bloowars Tools\Bloowars Tools\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

This is my code:

        public List<Player> PlayerList { get; set; }

    public A1_ResultWnd()
    {
        InitializeComponent();
        //GenerateColumns();            
    }

    private void GenerateColumns()
    {
        Grid.AutoGenerateColumns = false;

        DataGridViewColumn miejsce = new DataGridViewColumn();
        miejsce.DataPropertyName = "Miejsce";
        miejsce.HeaderText = "Miejsce";
        miejsce.Name = "miejsceCollumn";

        DataGridViewColumn imie = new DataGridViewColumn();
        miejsce.DataPropertyName = "Imie";
        miejsce.HeaderText = "Imię";

        DataGridViewColumn uid = new DataGridViewColumn();
        miejsce.DataPropertyName = "UID";
        miejsce.HeaderText = "UID";

        DataGridViewColumn rasa = new DataGridViewColumn();
        miejsce.DataPropertyName = "Rasa";
        miejsce.HeaderText = "Rasa";

        DataGridViewColumn sex = new DataGridViewColumn();
        miejsce.DataPropertyName = "Sex";
        miejsce.HeaderText = "Płeć";

        DataGridViewColumn klan = new DataGridViewColumn();
        miejsce.DataPropertyName = "Klan";
        miejsce.HeaderText = "Klan";

        DataGridViewColumn strefa = new DataGridViewColumn();
        miejsce.DataPropertyName = "Strefa";
        miejsce.HeaderText = "Strefa";

        DataGridViewColumn status = new DataGridViewColumn();
        miejsce.DataPropertyName = "Status";
        miejsce.HeaderText = "Status";

        Grid.Columns.Add(miejsce);
        Grid.Columns.Add(imie);
        Grid.Columns.Add(uid);
        Grid.Columns.Add(rasa);
        Grid.Columns.Add(sex);
        Grid.Columns.Add(klan);
        Grid.Columns.Add(strefa);
        Grid.Columns.Add(status);
    }

    private void A1_ResultWnd_Shown(object sender, EventArgs e)
    {
        GenerateColumns();
        Grid.DataSource = PlayerList; //Here the exception occures
    }

PlayreList is not empty.

like image 258
Hooch Avatar asked Dec 10 '11 13:12

Hooch


2 Answers

You need to give the type of the column. If you want a textBox column, you'd have to use new DataGridViewTextBoxColumn(). Let's say you want a textBox and a combobox, you'd have the followings:

also, you are calling the first column name over and over again.

    DataGridViewColumn miejsce = new DataGridViewTextBoxColumn();
    miejsce.DataPropertyName = "Miejsce";
    miejsce.HeaderText = "Miejsce";
    miejsce.Name = "miejsceCollumn";

Change the following for the given name.

    DataGridViewColumn imie = new DataGridViewComboBoxColumn();
    imie.DataPropertyName = "Imie";
    imie.HeaderText = "Imię";
    imie.Name = "imieCollumn"
like image 195
Gabriel GM Avatar answered Sep 25 '22 22:09

Gabriel GM


If DataGridView columns are created programmaticaly, then the AutoGenerateColumns property of the DataGridView have to be set on false.

My code is an example of a DataGridView able to display any DataTable and it is working perfectly in this form:

        // Prepare the DataViewGrid
        dataGridView1.Columns.Clear();

        // Add each column to the grid according to the data table structure
        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            DataGridViewColumn dataGridViewColumn = new DataGridViewColumn();
            DataGridViewCell dataGridViewCell = new DataGridViewTextBoxCell();
            dataGridViewColumn.DataPropertyName = dataTable.Columns[i].ColumnName;
            dataGridViewColumn.HeaderText = dataTable.Columns[i].ColumnName;
            dataGridViewColumn.CellTemplate = dataGridViewCell;
            dataGridViewColumn.Name = dataTable.Columns[i].ColumnName;
            dataGridView1.Columns.Add(dataGridViewColumn);
        }

        // Set the DataSource for the binding
        bindingSource1.DataSource = dataTable;
        // Prevent unwanted columns autogeneration
        dataGridView1.AutoGenerateColumns = false;
        // Provide the binding to the DataGridView
        dataGridView1.DataSource = bindingSource1;
like image 39
profimedica Avatar answered Sep 22 '22 22:09

profimedica