在窗体加载时在DataGridView中插入一次DataGridViewComboBoxColumn [英] insert DataGridViewComboBoxColumn once in a DataGridView on form load

查看:48
本文介绍了在窗体加载时在DataGridView中插入一次DataGridViewComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有此代码

I have this code below

cmb.HeaderText = "Promoted To"
cmb.Name = "cmb"
cmb.MaxDropDownItems = 4
cmb.items.add("Class 2")   
cmb.items.add("Class 3")
DataGridView.Columns.Insert(4, cmb)


此代码处于表单加载状态,每次我加载表单时,都会将DataGridViewComboBoxColumn插入位置4.但是我希望将DataGridViewComboBoxColumn插入一次,如果已经退出,则不应再次插入.


this code is at form load and each time i load the form, the DataGridViewComboBoxColumn is inserted at position 4.but i want the DataGridViewComboBoxColumn to be inserted once and if it already exit it should not be inserted again.

推荐答案

这没有道理.如果您不止一次加载"表单,那么您将创建该表单的新实例,而该实例不了解任何先前的实例.这意味着每个实例每次都应在位置4创建其组合列.

还是我错过了您在帖子中没有解释的内容?
This doesn''t make sense. If you''re "loading" the form more than once, you''re creating a new instance of it which has no knowledge of any previous instance. This means that every instance should be creating it''s combo column in position 4 every time.

Or am I missing something that you didn''t explain in your post?


如果您只想在首次页面加载时插入该组合,就可以将您的代码放入Not IsPostback循环中的页面加载量

如果不是IsPostBack

if you want the combo to be inserted only the first time the pageloads u can put ur code inside Not IsPostback loop of page load

If Not IsPostBack

cmb.HeaderText = "Promoted To"
cmb.Name = "cmb"
cmb.MaxDropDownItems = 4
cmb.items.add("Class 2")
cmb.items.add("Class 3")
DataGridView.Columns.Insert(4, cmb)



如果



End If


i有7列,那么当第一次加载表单时,由于cmb的插入,列数变为8.
因此我使用此代码来防止进一步插入cmb
i have 7 columns so when the form is loaded for the first time, the number of columns become 8 because of the insertion of the cmb.
so i use this code to prevent further insertion of cmb
If DataGridView.Columns.Count < 8 Then
                   Dim cmb As New DataGridViewComboBoxColumn()
                   cmb.HeaderText = "Promoted To"
                   cmb.Name = "cmb"
                   cmb.MaxDropDownItems = 4
                   DataGridView.Columns.Insert(4, cmb)
               End If


这篇关于在窗体加载时在DataGridView中插入一次DataGridViewComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆