数据库中DATABLE的性别列是数字表示,0为男,1为女现在我的winform的DataGridView 要绑定这个DATABLE,但性别列要自动显示 男和女! 怎么办?求完整代码方案

解决方案 »

  1.   

    select  case sex  when 0 then '男' when 1 then '女' end as sex  from table
    然后绑定~
      

  2.   

    可以自定义CellFormatting。如果需要编辑,则还要自定义CellParsing。        public Form1()
            {
                InitializeComponent();            DataTable table = new DataTable();
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Gender", typeof(int));
                table.Rows.Add("Alice", 0);
                table.Rows.Add("Bob", 1);            this.dataGridView1.DataSource = table;
                this.dataGridView1.Columns["Gender"].ReadOnly = true;
                this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
            }        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Gender" && e.Value is int)
                {
                    e.Value = (int)e.Value == 0 ? "女" : "男";
                    e.FormattingApplied = true;
                }
            }
      

  3.   

    data.Columns[2].Expression = "IIF(Column2='0', '男', '女')";
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
    {  
    if(this.dataGridView1.Columns[e.ColumnIndex].HeaderText =="sex")  
    {  
    if(object.Equals(e.Value,0))  
    {  
    e.Value=object.Equals(e.Value,0)?"男 ":"女 ";  
    }  
    }  
    }