我是用datagrid的setdatabinding方法实现运行时绑定;
格式如下:datagrid1.SetDataBinding(myds1,"tablename"),已知在myds1中的表tablename的字段名分别是:username,user_sex,我想实现以下功能,请问怎么解决?!!  -------------------------------------------------------------------  
  1). 在datagrid1对应的标题名变更:username→用户名、user_sex→性别;
  2). 能否对datagrid1中的每条记录某列进行判断?比如user_sex='b'时,在 
      datagrid1 性别=‘男’,user_sex='g',在datagrid1 性别='女';    
  -------------------------------------------------------------------
请各位指点!!

解决方案 »

  1.   

    datagrid1.DataSource=myds1["tablename"];
    至于更改标题,应打开“属性生成器”,取消“在生成时自动创建列”选项,然后手动添加每一列,并且为每一列指定名字。
      

  2.   

    thanks zhoufoxcn:
    请问"属性生成器"在哪?
      

  3.   

    to zhoufoxcn:
    在哪取消“在生成时自动创建列”?
      

  4.   

    把datagrid的autogenerateColumns设置为false 然后自己添加列,设定标题
    在datagrid上 右键->属性
      

  5.   

    thanks to babyrockxray:
    我用的是vs2003,好像没有autogenerateColumns这个属性?
      

  6.   

    临时解决方案.....COPY数据源,然后改你的副本中的列名为指定名,然后遍历,把改动好的副本绑定到DG
      

  7.   

    第一个好了
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
    this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
    this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(24, 16);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(248, 240);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
      this.dataGridTableStyle1});
    // 
    // dataGridTableStyle1
    // 
    this.dataGridTableStyle1.DataGrid = this.dataGrid1;
    this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
      this.dataGridTextBoxColumn1,
      this.dataGridTextBoxColumn2});
    this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGridTableStyle1.MappingName = "table";
    // 
    // dataGridTextBoxColumn1
    // 
    this.dataGridTextBoxColumn1.Format = "";
    this.dataGridTextBoxColumn1.FormatInfo = null;
    this.dataGridTextBoxColumn1.HeaderText = "名字";
    this.dataGridTextBoxColumn1.MappingName = "username";
    this.dataGridTextBoxColumn1.Width = 75;
    // 
    // dataGridTextBoxColumn2
    // 
    this.dataGridTextBoxColumn2.Format = "";
    this.dataGridTextBoxColumn2.FormatInfo = null;
    this.dataGridTextBoxColumn2.HeaderText = "性别";
    this.dataGridTextBoxColumn2.MappingName = "user_sex";
    this.dataGridTextBoxColumn2.Width = 75;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    //定义格式
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.TableName = "table";
    ds.Tables.Add(dt);
    DataColumn dc = new DataColumn();
    dc.ColumnName = "username";
    dt.Columns.Add(dc);
    DataColumn dc1 = new DataColumn();
    dc1.ColumnName  = "user_sex";
    dt.Columns.Add(dc1);

    //数据
    DataRow dr = dt.NewRow();
    dr[0] ="roro";
    dr[1] = "b";
    dt.Rows.Add(dr);
    DataRow dr1 = dt.NewRow();
    dr1[0] = "yuner";
    dr1[1] = "g";
    dt.Rows.Add(dr1);
    //bind
    this.dataGrid1.SetDataBinding(ds,"table");
      

  8.   

    dataGrid 没用过。 adataGridview中,有个CellFormatting可以解决第2个问题,不知道dataGrid 有没。
      另外:(转载自:zhzuo(秋枫) )
          在绑定的时候,访问对应的Binding对象, 
          使用Binding.Format 事件 和Binding.Parse事件  来处理显示值或保存到数据库的值。
          另一种方式通过sql语句直接在数据库中返回记录时进行转换判断返回。
          使用ADO.NET 中的表达式也是一中不错的途径。
          http://www.microsoft.com/china/MSDN/library/data/dataAccess/ADONETEXP.mspx
      

  9.   

    自定义样式吧.就象 atlasroben(南宫天天) 所写
      

  10.   

    protected void dataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (e.Item.Cells[0].Text.Trim() == "b")
                        e.Item.Cells[0].Text = "男";
                    else if (e.Item.Cells[0].Text.Trim() == "g")
                        e.Item.Cells[0].Text = "女";            }        }