本文介绍了至少 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:DocumentsVisual Studio 2010ProjectsBloowars ToolsBloowars ToolsA1_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:DocumentsVisual Studio 2010ProjectsBloowars ToolsBloowars ToolsForm1.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:DocumentsVisual Studio 2010ProjectsBloowars ToolsBloowars ToolsProgram.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 不为空.
PlayreList is not empty.
推荐答案
你需要给出列的类型.如果你想要一个 textBox 列,你必须使用 new DataGridViewTextBoxColumn()
.假设您想要一个文本框和一个组合框,您将拥有以下内容:
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"
这篇关于至少 DataGridView 控件的一列没有单元格模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!