private void FTPTestForm_Load(object sender, EventArgs e)
        {
            BindingSource bs = null;
            DataTable dtData = GetData();//dtData(ID,Name,Sex);
            bs = new BindingSource();
            bs.DataSource = dtData;
            this.dataGridView1.DataSource = bs;
        }        private DataTable GetData()
        {
            DataTable table = new DataTable();
            DataColumn column;
            DataRow row;            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Sex";
            column.ReadOnly = true;
            column.Unique = true;
            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);            // Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Name";
            column.AutoIncrement = false;
            column.Caption = "Name";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["id"];
            table.PrimaryKey = PrimaryKeyColumns;            for (int i = 0; i < 2; i++)
            {
                row = table.NewRow();
                if(i==0)
                    row["Sex"] = "女";
                if(i==1)
                    row["Sex"] = "男";

                row["Name"] = "Name " + i;
                table.Rows.Add(row);
            }
            return table;
        }
参考!

解决方案 »

  1.   


                    if(i==0)
                        row["Sex"] = "女";
                    if(i==1)
                        row["Sex"] = "男";

    主要是这里
      

  2.   

    思路就是说你用自己的方法去读Sex值,根据判断结果显示不同内容
      

  3.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == SexColumn.Index)
                    if (e.Value == 0)
                        e.Value = "男";
                    else if (e.Value == 1)
                        e.Value = "女";
                    else if (e.Value == 2)
                        e.Value = "人妖";
                    else
                        e.Value = String.Empty;
            }
      

  4.   


    有没有仔细看过我的代码啊。Cell_Formatting事件是在datagridview的单元格值显示前的格式化处理时的时候触发的,是确定取出的数据具体怎么去显示。而不会修改bs的原始数据
      

  5.   


    你说的不对,bs中的数据是要被更改为“男”这个值的,不信你自己测测,而我的期望是被修改为1.而不是修改为男。  意思就是说
    从数据库取出两个人放到bs中
    A(nameA,25,1):
    B(nameB,23,0);
    绑定到datagridview显示为
    nameA  25  男
    nameB  23  女现在把nameA改为女以后期望bs中得到
    nameA,25,0这个结果,然而实际bs中得到的结果是
    nameA,25,女。
      

  6.   

    晕,你哪里自己改掉的吧using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication41
    {
        public partial class Form1 : Form
        {
            DataTable DT = null;
            
            public Form1()
            {
                InitializeComponent();
         
                this.Controls.Clear();
                this.StartPosition = FormStartPosition.CenterScreen;
                
                DT = new DataTable();
                DT.Columns.Add(new DataColumn("sex"));
                DT.Rows.Add(new Object[] { 0 });
                DT.Rows.Add(new Object[] { 1 });
         
                DataGridView DGV = new DataGridView();
                DGV.Parent = this;
                DGV.Dock = DockStyle.Fill;
                DataGridViewTextBoxColumn DGVC = new DataGridViewTextBoxColumn();
                DGVC.DataPropertyName = "sex";
                DGV.Columns.Add(DGVC);
                DGV.CellFormatting += new DataGridViewCellFormattingEventHandler(DGV_CellFormatting);
                DGV.DataSource = DT;            Button B = new Button();
                B.Text = "查看值";
                B.Parent = this;
                B.Click += new EventHandler(B_Click);
                B.BringToFront();
            }        void B_Click(object sender, EventArgs e)
            {
                MessageBox.Show(DT.Rows[0][0].ToString() + " " + DT.Rows[1][0].ToString()); // 输出: 0 1
            }        void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (Convert .ToInt32 ( e.Value) == 0)
                    e.Value = "男";
                else if (Convert .ToInt32 (e.Value )== 1)
                    e.Value = "女";
                else
                    e.Value = String.Empty;
            }
        }
    }