简体   繁体   中英

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

I have a datagridview, bound to data and running 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. So I want to hide column 2 and 3 and interpret back and forth from 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.

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. I cannot see how that would work though.

From Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET :

There are two primary ways to populate the contents of unbound columns: handling the RowsAdded event or handling the CellFormatting event. 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.

Yes it is possible.

First set AutoGenerateColumns="False" , then add 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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