在数据绑定数据网格视图中添加按钮列

时间:2011-05-22 04:02:55

标签: winforms forms datagridview

我有一个datagridview。我把它绑定到一个列表。现在我想在它的末尾显示一列。但是这个专栏的内容是错误的。

这是我的代码

    grdPatientAppointment.DataSource = lst;


        grdPatientAppointment.Columns["ID"].Visible = false;
        //grdPatientAppointment.Columns["AdmitDate"].Visible = false;
        //grdPatientAppointment.Columns["DischargeDate"].Visible = false;
        grdPatientAppointment.Columns["AppointmentID"].Visible = false;

        grdPatientAppointment.Columns["PatientrName"].DisplayIndex = 0;
        grdPatientAppointment.Columns["Age"].DisplayIndex = 1;
        grdPatientAppointment.Columns["Address"].DisplayIndex = 2;
        grdPatientAppointment.Columns["ContactNo"].DisplayIndex = 3;
        grdPatientAppointment.Columns["Dieseas"].DisplayIndex = 4;
        grdPatientAppointment.Columns["AppointmentDate"].DisplayIndex = 5;

        DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
        btnColumn.HeaderText = "Treat";
        btnColumn.Text = "Treat";
        btnColumn.UseColumnTextForButtonValue = true;            
        grdPatientAppointment.Columns.Insert(6,btnColumn);

这里是输出:

here is output

但我希望该按钮到数据网格视图的末尾

4 个答案:

答案 0 :(得分:4)

添加列而不是将其插入GridView。它会自动将它附加到列集合的末尾。

    grdPatientAppointment.Columns.Add(btnColumn);

答案 1 :(得分:2)

答案 2 :(得分:0)

只需在

下添加代码即可
grdPatientAppointment.Columns.Insert(I, btnColumn)

我是您要添加的列的索引

答案 3 :(得分:0)

使用grdPatientAppointment.AutoGenerateColumns = false;

然后添加网格将从DataSource接收的所有列,并从编辑器绑定它们。