SQL Server 2008 中从数据库到数据集的位数据类型到枚举类型的映射 [英] Bit datatype to enum type mapping from database to dataset in SQL Server 2008

查看:22
本文介绍了SQL Server 2008 中从数据库到数据集的位数据类型到枚举类型的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,它有一个列 Xyz 并且它在 SQL Server 2008 中具有 bit 数据类型.

I have a table which has a column Xyz and it has bit datatype in SQL Server 2008.

我通过数据适配器从表中获取值,并将其存储在 DataSet 中,DataGridView 将显示 中的内容数据集

I'm getting the values from the table through data adapter and I'm storing it in a DataSet and a DataGridView will show the contents from the DataSet

在网格视图中,对于列 Xyz,会显示一个带/不带刻度的复选框,但我想将其显示为作为文本而不是复选框的买入/卖出.

In the gridview, for column Xyz, a check_box with/without tick is displayed but I want to display it as a buy/sell as a text instead of checkbox.

推荐答案

您可以通过 2 种方式中的 1 种方式来处理它.

You can handle it in 1 of 2 ways.

1) 不要将数据返回为位,而是在查询中进行强制转换,使其根据值将 Buy/Sell 作为字符串返回.只有当您的网格是只读的时,这才会真正有效.如果您需要能够添加/编辑数据,那么将您的买入/卖出转换回位并强制用户只能输入买入/卖出会变得混乱.如果您需要添加/编辑数据,您可能希望使用方法 2.

1) Instead of returning the data as a bit, do the casting in your query to have it return Buy/Sell as a string based on the value. This will only really work well if your grid is read-only. If you need to be able to add/edit data, it would get messy to convert your Buy/Sell back to a bit and enforce that the user could only enter buy/sell. You'd probably want to use method 2 if you need to add/edit data.

例如假设您的列名称为 BuySell,类型为 bit

e.g. let's say your column name is called BuySell and is of type bit

SELECT CASE WHEN BuySell = CAST(0 AS BIT) THEN 'Buy' ELSE 'Sell' AS BuySell FROM TableName

2) 您必须关闭 DataGridView 上的Autogeneratecolumns"并手动设置列.如果您的网格是只读的,我会为您的买入/卖出列添加一个文本列,该列映射到您的位值.然后在网格的 Cell_Formatting 事件中,根据位更新值.类似于以下内容:

2) You will have to turn off "Autogeneratecolumns" on the DataGridView and setup your columns manually. If your grid is read-only, I'd add a text column for your buy/sell column that maps to your bit value. Then in the Cell_Formatting event for the grid, update the value based on the bit. Something like the below:

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dgv.Columns[e.ColumnIndex].Name == "buysell")
    {
        if (e.Value != null)
        {
            if (e.Value.ToString() == "1")
            {
                e.Value = "Sell";
            }
            else
            {
                e.Value = "Buy";
            }
        }
        else
        {
            e.Value = "Buy";
        }
    }
}

如果您的网格需要可编辑,请设置一个表示您的买入/卖出值的数据表,其中包含显示成员和值成员.将其绑定为组合框列的数据源.现在加载数据将在组合框中正确显示买入/卖出,对于新行,当您从下拉列表中选择一个值时,它将使用正确的位值填充您的基础数据源.

If your grid needs to be editable, setup a DataTable that represents your Buy/Sell values with a displaymember and valuemember. Bind that as the datasource for a combobox column. Now loading the data will correctly display Buy/Sell in the combobox and for new rows when you select a value from the drop-down it will populate your underlying datasource with the correct bit value.

这篇关于SQL Server 2008 中从数据库到数据集的位数据类型到枚举类型的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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