繁体   English   中英

将数据源分配给DataGrid之后如何添加未绑定的按钮列

[英]How to add unbound button column after assigning datasource to DataGrid

我有以下代码将datasource分配给DataGridView

Private Sub LoadDataForDataGrid()
        setconnection()
        Dim ds1 As New DataSet()
        cmd.CommandText = "select tj.jb_item_id as [Job Item Id], tj.jb_item_no as [Part #], tj.jb_item_name as [Part name], ti.Unit_price as Price, tj.jb_job_item_qty as Quantity, tj.jb_job_total as [Total Price] from tbl_jobitem tj, tblitementry_list ti where tj.jb_job_card_id='" & JobCardID & "' and ti.Item_no=tj.jb_item_no"
        adp4.SelectCommand = cmd
        adp4.Fill(ds1)
        adp4.Dispose()
        cmd.Dispose()
        cnn.Close()
        Me.DGServiceDetails.DataSource = ds1.Tables(0)
        CreateUnboundButtonColumn()//this will create an extra column with the added rows
End Sub

现在,由于我想在每行上添加Delete功能,并在每行上都带有Delete button ,因此在将每条记录添加到表tbl_jobitem之后调用上述函数,下面是我从此源获取CreateUnboundButtonColumn方法

Private Sub CreateUnboundButtonColumn()

        Dim buttonColumn As New DataGridViewButtonColumn

        With buttonColumn
            .HeaderText = "Action"
            .Name = "Delete"
            .Text = "Delete"
        End With
        Me.DGServiceDetails.Rows.Insert(0, buttonColumn)
End Sub

但上面的函数添加删除每个呼叫按钮与额外的列添加每次到各行,从而增加列数与delete button各一次。 我尝试通过单击“ Edit Columns并将其添加为DataGridViewButtonColumn来将其添加为设计部分,效果很好,并向每行添加了“删除”按钮,但是我无法将事件处理程序附加到该行。 因此,对此问题的任何想法或想法都将受到高度赞赏。

我在这里标记C#,因为也接受了使用C#的解决方案

添加未绑定列

要添加未绑定列:

var deleteButton=new DataGridViewButtonColumn();
deleteButton.Name="dataGridViewDeleteButton";
deleteButton.HeaderText="Delete";
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
this.dataGridView1.Columns.Add(deleteButton);

设置未绑定按钮列的文本

要在每个按钮上显示“删除”文本:

deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;

同样作为另一个选项,特别是当您想要将按钮的文本设置为不同的值时,可以使用DataGridView CellFormatting事件并设置这些单元格的值:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //If this is header row or new row, do nothing
    if (e.RowIndex < 0 || e.RowIndex == this.dataGridView1.NewRowIndex)
        return;

    //If formatting your desired column, set the value
    if (e.ColumnIndex=this.dataGridView1.Columns["dataGridViewDeleteButton"].Index)
    {
        e.Value = "Delete";
    }
}

处理点击单元格

您可以处理DataGridView CellClick事件,例如:

void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //if click is on new row or header row
    if( e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
        return;

    //Handle Button Column Click
    if( e.ColumnIndex  == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
    {
        //Do the stuff for first button click
    }
}

事件何时发生?

  1. 单击单元格时,将发生此事件。
  2. 当用户在按钮单元格或复选框单元格具有焦点的情况下按下并释放空格键时,也会发生这种情况。

如何检查我想要的单元格是否单击?
例如,如果知道所需列的索引为4,则可以检查:

if( e.ColumnIndex  == 4)
{ 
    //Do the stuff for your desired button click
    MessageBox.Show("Button Clicked");         
}

例如,如果知道所需列的名称是dataGridViewDeleteButton,则可以检查:

if( e.ColumnIndex  == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
     //Do the stuff for your desired button click
}

在哪里可以找到点击的单元格行的数据?

//Here is the row object
var row = this.dataGridView1.Rows[e.RowIndex]

//Value of cell 0 of the row
this.dataGridView1.Rows[e.RowIndex].Cells[0].Value

//Value of IdTextBoxColumn cell
this.dataGridView1.Rows[e.RowIndex].Cells["IdTextBoxColumn"].Value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM