简体   繁体   English

Datagridview编辑/保存按钮

[英]Datagridview edit/save button

I have a datagridview table displaying some customer information from an SQL table. 我有一个datagridview表,该表显示来自SQL表的一些客户信息。 I have added a datagridviewbuttoncolumn on the end to allow editing of a single row at a time. 我在最后添加了一个datagridviewbuttoncolumn ,以允许一次编辑单个行。 However, I cannot even get basic functionality to work with the button. 但是,我什至无法获得使用该按钮的基本功能。 The UseColumnTextForButton property doesn't seem to work, nor when I progrommatically try to change the text. UseColumnTextForButton属性似乎不起作用,也没有在我通过编程方式尝试更改文本时起作用。

private void CardList_grdvw_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var senderGrid = (DataGridView)sender;

        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
            e.RowIndex >= 0)
        {
            senderGrid.Rows[e.RowIndex].ReadOnly = false;
            var btn = (DataGridViewButtonCell)senderGrid.CurrentCell;
            btn.Value = "save";
        }
    }

I have checked to verify that the correct cell is being referenced. 我已检查验证是否引用了正确的单元格。 When debugging, after I set btn.Value = "save" it still is null . 调试时,在设置btn.Value = "save"它仍然为null

Additionally, the row is still un-editable even after setting its ReadOnly property to false . 此外,即使将其ReadOnly属性设置为false ,该行仍不可编辑。

Datagridview wizard Datagridview向导

By default, the Text property of a column in the DataGridView is blank. 默认情况下,DataGridView中列的Text属性为空白。 I think you are confusing the HeaderText and Text properties. 认为您在混淆HeaderText和Text属性。 When you set the UseColumnTextForButton to true, the buttons will use the Text property of the column. 当您将UseColumnTextForButton设置为true时,按钮将使用列的Text属性。

Make sure you supply that Text property in the designer. 确保在设计器中提供了Text属性。

To make the code you posted work, set the UseColumnTextForButton back to false. 要使发布的代码正常工作,请将UseColumnTextForButton设置回false。

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

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