博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于DataGridView绑定数据源后未设置的列不显示

Posted on 2011-07-21 23:30  itcfj  阅读(1652)  评论(0编辑  收藏  举报

把datagridview 数据源转换为绑定的数据源

    public class PlanInfo
    {
        /// <summary>
        /// 计划ID
        /// </summary>
        public int a { get; set; }
        /// <summary>
        /// 序号
        /// </summary>
        public int b { get; set; }
        /// <summary>
        /// 发车
        /// </summary>
        public string c { get; set; }

    }

List<PlanInfo> planInfos1 = new List<PlanInfo>();
dataGridview 绑定planInfos1,显示列为计划ID,序号。现访问其他列的数据
 List<PlanInfo> ps = dataGridview.DataSource as List<PlanInfo>;
                PlanInfo p = ps[e.RowIndex];

                    this.c = p.a;
                    this.c = p.c;
                 
       private void dataGridview_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {
            foreach (DataGridViewRow dr in this.dgvOnVehicle.Rows)
            {
                dr.Selected = false;
            }
            this.dataGridview.Rows[e.RowIndex].Selected = true;
            DataGridView dgv = (DataGridView)sender;
            List<PlanInfo> ps = dgv.DataSource as List<PlanInfo>;
            if (ps != null)
            {
                PlanInfo p = ps[e.RowIndex];
                if (p == null) return;


                    planInfoMenu = p;

                    this.a = p.a;
                    this.b = p.b;
                    this.c = p.c;
                  
                }
                catch (Exception ex)
                {
                }
            }
        }