C# winform关于datagridview中的列的数据类型转换问题
今天有一同学问到关于datagridvie中某一列的数据在数据库中的类型为int型!放到datatable中绑定到datagridview中!想把数据中的1or0的数据该为“是”or“否”!在网上查了半天资料没有查到自己想要的!老是不能将int型转成string。报异常。功夫不负有心人!还是解决了这个问题。问题关键就是在cellformating事件中重新格式化单元格,设置自己想要的样式数据。asp.net中有一个控件和datagridview相似dataview它有一个这样的行绑定事件,可以很容易解决这个问题!winform不太熟悉,解决这个问题浪费了不少时间!写完这个还报一个错。异常。解决的方法就是将datagridview中的自动添加列的勾去掉就可以了,也就是将空白的那一行去掉。oK!参考资料:http://www.informit.com/articles/article.aspx?p=446453&seqNum=5
下面是这个实例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“yuXiangUsomDataSet.Cd_DeskName”中。您可以根据需要移动或移除它。
this.cd_DeskNameTableAdapter.Fill(this.yuXiangUsomDataSet.Cd_DeskName);
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex ==4)
{
e.FormattingApplied = true;
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if(row!=null){
if (row.Cells[4].Value.ToString() == "1")
{
e.Value = string.Format("{0}",
"好啊");
}
else {
e.Value = string.Format("{0}",
"不好");
}
}
}
}
}
}
posted on 2010-01-25 22:00 tongdengquan 阅读(947) 评论(0) 编辑 收藏 举报