在 DataGridView 中,添加新行时将列的 ReadOnly 属性设置为 false,更新其 true (c#.net)

发布于 2024-12-10 13:20:20 字数 610 浏览 0 评论 0原文

我已将 2 个数据表列的只读属性设置为 true。

    List.Columns[0].ReadOnly = true;
    List.Columns[1].ReadOnly = true;

但我只希望它们仅在用户尝试更新时只读,用户可以向 dataGridView 添加新行,因此我想在尝试添加新行时将 readonly 属性设置为 false。我尝试在数据网格的 CellDoubleClick 事件上执行此操作,但它不会执行任何操作,因为调用 beginedit 为时已晚。

if(e.RowIndex == GridView.Rows.Count-1)
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = false;
            else
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = true;

任何想法

I have set the readonly property of 2 datatable columns to true.

    List.Columns[0].ReadOnly = true;
    List.Columns[1].ReadOnly = true;

But i only want them to be read only when user is trying to update, User can add new rows to dataGridView so i want to turn the readonly property to false when trying to add new row. i tried doing this on CellDoubleClick event of the datagrid but it wont do anything as it is to late for the beginedit to be called.

if(e.RowIndex == GridView.Rows.Count-1)
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = false;
            else
                GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = true;

Any ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-12-17 13:20:20

您必须使用 cellbegin 编辑将单元格只读属性设置为 true。 。

   private  void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
   {
       if (dataGridView1.Columns[e.ColumnIndex].Name == "ColName0")
       {
           // you can check whether the read only property of that cell is false or not

       }
   }

我希望它能帮助你...

you Have to use the cellbegin edit to make the cell readonly property to true. .

   private  void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
   {
       if (dataGridView1.Columns[e.ColumnIndex].Name == "ColName0")
       {
           // you can check whether the read only property of that cell is false or not

       }
   }

I hope it will helps you...

情深缘浅 2024-12-17 13:20:20

听起来您想要做的是将网格中的所有行设为只读,除非它们是新行,这意味着无法编辑创建的行。如果这是正确的,那么您可以做的就是在 DataBindingComplete 事件期间将该行设置为只读,如下所示:

dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (!item.IsNewRow)
            item.ReadOnly = true; 
    }
}

重要的部分是检查该行是否是新行。

It sounds like what you want to do is make all the rows in the grid readonly unless they are the new row, thus meaning created rows cannot be edited. If that is correct then what you can do is set the row to readonly during the DataBindingComplete event like so:

dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (!item.IsNewRow)
            item.ReadOnly = true; 
    }
}

The important part is the check to see if the row is the new row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文