简体   繁体   English

如何为DataGridView创建CheckedListBox列

[英]How to create a CheckedListBox column for a DataGridView

What is the easiest way to create a DataGridView column that contains a CheckedListBox control for each cell? 创建包含每个单元格的CheckedListBox控件的DataGridView列的最简单方法是什么? The CheckedListBox does not need to be databound. CheckedListBox不需要进行数据绑定。

For winforms, in the past I think I used to do it this way: 对于winforms,过去我认为我以前是这样做的:

Either, edit columns for the grid view and add a checkbox column or you can go into the designer of the form and put the following: 编辑网格视图的列并添加复选框列,或者可以进入表单的设计器并输入以下内容:

private System.Windows.Forms.DataGridViewCheckBoxColumn Column1;//Where column1 is the name of the column.

I don't know about the easiest, but you would probably want to create a custom object that inherits from DataGridViewCell, and also create a control containing your CheckedListBox. 我不知道最简单的方法,但是您可能想创建一个继承自DataGridViewCell的自定义对象,并创建一个包含CheckedListBox的控件。 Then override the Paint method inside of your custom object and have it draw in the control containing the CheckedListBox. 然后,在您的自定义对象中重写Paint方法,并将其绘制在包含CheckedListBox的控件中。

Once all of that is done, create a DataGridViewColumn, and set the CellTemplate property to the custom DataGridViewCell object you've created. 完成所有这些操作后,创建一个DataGridViewColumn,并将CellTemplate属性设置为您创建的自定义DataGridViewCell对象。

Add a Template Column and then Edit templet column from start tag. 添加模板列,然后从开始标签编辑模板列。 Drag and drop a checkbox into it. 将一个复选框拖放到其中。 you can remove the lable and text box that is created when a databount is modified to Template. 您可以删除将databount修改为Template时创建的标签和文本框。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>                
                <asp:TemplateField>                   
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>                
            </Columns>
        </asp:GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM