简体   繁体   English

是否可以将未绑定的列插入绑定的datagridview?

[英]Is it possible to insert unbound column into a bound datagridview?

I have a datagridview, bound to data and running swell. 我有一个datagridview,绑定到数据并运行swell。 The data in two columns are integers, but they have a meaning that I would like the form user to see instead. 两列中的数据是整数,但它们具有我希望表单用户可以看到的含义。

Column 1       Column 2       Column 3       Column 4
_____________________________________________________
Bob            1              2               Yes
Mary           1              3               No
Tod            2              2               No
Beth           3              3               Yes

Where 1, 2, and 3 are something like job skills. 其中1,2和3类似于工作技能。 So I want to hide column 2 and 3 and interpret back and forth from SQL. 所以我想隐藏第2列和第3列并从SQL来回解释。

Column 1       Column 2       Column 3       Column 2a        Column 3a        Column 4
_______________________________________________________________________________________
Bob            1              2              Accounting      Admin            Yes
Mary           1              3              Accounting      Manager          No
Tod            2              2              Architect       Admin            No
Beth           3              3              IT              Manager          Yes

Only imagine Columns 2 & 3 are hidden and maybe 2a & 3a are comboboxes that have code behind them that convert the strings to the integers. 只想象第2和第3列是隐藏的,也许2a和3a是组合框,它们后面有代码将字符串转换为整数。

The project is too large to fix it to use strings in the first place. 该项目太大,无法修复它首先使用字符串。 I'm open to other suggestions like make Columns 2 & 3 comboboxes that are both databound and function based on another SQL command. 我对其他建议持开放态度,比如使用另一个SQL命令来实现数据绑定和功能的第2列和第3组合框。 I cannot see how that would work though. 我看不出它会如何起作用。

From Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET : Windows Forms 2.0的数据绑定:使用.NET编程智能客户端数据应用程序

There are two primary ways to populate the contents of unbound columns: handling the RowsAdded event or handling the CellFormatting event. 有两种主要的方法来填充未绑定列的内容:处理RowsAdded事件或处理CellFormatting事件。 The former is a good place to set the cell's value to make it available for programmatic retrieval later. 前者是设置单元格值的好地方,以便以后可用于编程检索。 The latter is a good place to provide a value that will be used for display purposes only and won't be stored as part of the data retained by the grid cells collection. 后者是提供仅用于显示目的的值的好地方,并且不会存储为网格单元集合保留的数据的一部分。 The CellFormatting event can also be used to transform values as they are presented in a cell to something different than the value that is actually stored in the data that sits behind the grid. CellFormatting事件还可用于将值在单元格中呈现时转换为与实际存储在网格后面的数据中的值不同的值。

Yes it is possible. 对的,这是可能的。

First set AutoGenerateColumns="False" , then add TemplateFields : 首先设置AutoGenerateColumns =“False” ,然后添加TemplateFields

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:boundfield datafield="Column1"  HeaderText="Column 1"  SortExpression="Column1"/>
        <asp:boundfield datafield="Column2"  HeaderText="Column 2"  SortExpression="Column2"/>
        <asp:TemplateField HeaderText="Column 4">
            <ItemTemplate>    
               <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:boundfield datafield="Column3"  HeaderText="Column 3"  SortExpression="Column3"/>
    </Columns>
</asp:GridView>

You can then change the values of each of the TemplateFields during the GridView1_RowDataBound method. 然后,您可以在GridView1_RowDataBound方法期间更改每个TemplateField的值。

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

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