zoukankan      html  css  js  c++  java
  • 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}",
                      "不好");
                    }
                    }
                }

            }
        }
    }

  • 相关阅读:
    FZU 1005 Fast Food(dp)
    POJ 3186 Treats for the Cows(区间DP)
    2016郑州轻工业学院校赛 B 蛤玮的财宝
    c++大数模板
    2015轻院校赛 H五子棋
    poj 1015 Jury Compromise
    modbus协议说明(转)
    STM32 flash 内存分布介绍
    STM32 程序所占用空间计算 && FLASH存储的起始地址计算
    C float与char数组 互转
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090610.html
Copyright © 2011-2022 走看看