web-dev-qa-db-fra.com

Au moins une des colonnes du contrôle DataGridView n'a pas de modèle de cellule

Je reçois cette 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: 

Ceci est mon 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 n'est pas vide.

19
Hooch

Vous devez donner le type de la colonne. Si vous voulez une colonne textBox, vous devez utiliser new DataGridViewTextBoxColumn(). Disons que vous voulez une zone de texte et une liste déroulante, vous obtiendrez les éléments suivants:

de plus, vous appelez le nom de la première colonne encore et encore.

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

Modifiez les éléments suivants pour le nom donné.

    DataGridViewColumn imie = new DataGridViewComboBoxColumn();
    imie.DataPropertyName = "Imie";
    imie.HeaderText = "Imię";
    imie.Name = "imieCollumn"
52
Gabriel GM

Si des colonnes DataGridView sont créées par programme, la propriété AutoGenerateColumns de DataGridView doit être définie sur false. 

Mon code est un exemple de DataGridView capable d'afficher n'importe quel DataTable et il fonctionne parfaitement sous cette forme:

        // 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;
5
profimedica

Je devais juste définir la CellTemplate de la colonne pour résoudre le problème.

DataGridViewColumn c = new DataGridViewColumn();
c.Name = "ColumnName";
c.HeaderText = "DisplayText";
c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
c.CellTemplate = new DataGridViewTextBoxCell();
dataGridView1.Columns.Add(c);
0
Baddack