至少有一个DataGridView控件的列没有单元格模板

时间:2011-12-10 13:29:36

标签: c# winforms exception collections datagridview

我得到了那个例外。

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: 

这是我的代码:

        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不为空。

4 个答案:

答案 0 :(得分:53)

您需要提供列的类型。如果您需要textBox列,则必须使用new DataGridViewTextBoxColumn()。假设您想要一个textBox和一个组合框,您将拥有以下内容:

另外,你一遍又一遍地调用第一个列名。

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

更改给定名称的以下内容。

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

答案 1 :(得分:5)

如果以编程方式创建DataGridView列,则必须将DataGridView的AutoGenerateColumns属性设置为false。

我的代码是DataGridView的一个示例,它能够显示任何DataTable,并且它以这种形式完美运行:

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

答案 2 :(得分:0)

我只需要设置列的CellTemplate即可解决问题。

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

答案 3 :(得分:0)

您可以使用以下代码通过设置CellTemplate在网格中添加列:

Grid.Columns.Add(new DataGridViewColumn() { CellTemplate = new 
DataGridViewTextBoxCell() , HeaderText = "Miejsce" , Name = "miejsceCollumn" ,  
AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells });