从SQL Server 2008中的数据库到数据集的位数据类型到枚举类型映射 [英] Bit datatype to enum type mapping from database to dataset in SQL Server 2008
问题描述
我有一个表,它有一个列 Xyz
,它在SQL Server 2008中有位
数据类型。 p>
我通过数据适配器从表中获取值,并将其存储在 DataSet
和 DataGridView
将显示 DataSet中的内容
gridview,对于列 Xyz
,显示带有/不带勾号的check_box,但是我要显示为买/卖作为文本而不是复选框。
您可以通过两种方式之一处理它。
数据作为位,在你的查询中进行转换,让它基于值返回买入/卖出作为字符串。这只会真正工作,如果你的网格是只读的。如果您需要能够添加/编辑数据,那么将您的买入/卖出转换为有点并强制用户只能输入买入/卖出将是麻烦的。如果您需要添加/编辑数据,则可能需要使用方法2.
例如。让我们假设你的列名称叫做BuySell,类型为
SELECT CASE WHEN BuySell = CAST(0 AS BIT)购买'ELSE'卖'从购买
2)您必须关闭自动生成列并在DataGridView上手动设置列。如果您的网格是只读的,我会为您的买/卖列添加一个映射到您的位值的文本列。然后在网格的Cell_Formatting事件中,根据该位更新值。如下所示:
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;
}
}
}
如果您的网格需要可编辑,使用显示成员和估价人员设置表示您的买入/卖出价值的数据表。将其绑定为组合框列的数据源。现在加载数据将在组合框中正确显示Buy / Sell,而当您从下拉列表中选择一个值时,它将以正确的位值填充您的底层数据源。
I have a table which has a column Xyz
and it has bit
datatype in SQL Server 2008.
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
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.
You can handle it in 1 of 2 ways.
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.
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) 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屋!