数据 ID  Name  Display
1     nick       1
2     tom       2
Display 是字典  public static readonly Dictionary<int, string> DictDisplay = new Dictionary<int, string>(6)
        {
            { 0, "删除" },
            { 1, "屏蔽" },
            { 2, "显示" } 
        }; var  t = Model.Context.Tests.Where(it=>it.Display>=1);//获取数据
 gvinfo.DataSource = t;//绑定  gvinfo 为datagridview
现在显示的是
1     nick       1
2     tom       2
要显示为
1     nick       屏蔽
2     tom       显示
请问如何写

解决方案 »

  1.   


            private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
            {
                int i = 0;
                if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(), out i))
                {
                    dataGridView1.Rows[e.RowIndex].Cells[2].Value = DictDisplay[i];
                }
            }
      

  2.   

    楼上是1种另外一种是在 单元格fomart事件中处理dataGridView1.CellFormatting += new
            DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
      

  3.   

    1楼  ojlovecd:
    dataGridView1.Rows[e.RowIndex].Cells[2].Value = DictDisplay[i];
    出现了错误   
    ---------------------------
    “DataGridView 默认错误”对话框
    ---------------------------
    DataGridView 中发生以下异常:
    System.Exception: 显示 不是 Int32 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。
       在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       在 System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo)
       在 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)   --- 内部异常堆栈跟踪的结尾 ---
       在 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       在 System.ComponentModel.TypeConverter.ConvertFrom(Object value)
       在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)
      

  4.   

    自己解决 private void gvinfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 2) //哪一列
                {
                    e.Value = Sys.DictDisplay[(int)e.Value];
                }
            }
      

  5.   

    试试换成这样的LINQ查询,手写的:
    var result = DictDisplay.GroupJoin(Model.Context.Tests, 
                                                            i=>i.Key,
                                                            o=>o.Display,
                                                            (i, os) => os.DefaultIfEmpty().Select(r => new
                                                                                    {
                                                                                                id = r.ID,
                                                                                                name = r.Name,
                                                                                                display = i.Value
                                                                                     })).SelectMany(m => m);