【问题标题】:How to display the default values in datagridview for multiple rows如何在datagridview中显示多行的默认值
【发布时间】:2013-10-23 07:43:33
【问题描述】:

我有一个 Windows 窗体中的 datagridview,我必须显示 5 行数据。

我正在使用此代码...

private void DGV_ActivityDtls_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{        
   e.Row.Cells[0].Value = "Activities";
   e.Row.Cells[1].Value = " ";
   e.Row.Cells[2].Value = "LT";
}

对于一行它工作正常,但我还有 4 行。 我是新手,谁能帮帮我?

提前致谢。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    DefaultValuesNeeded 仅在用户或DataGridView 创建新的默认行时触发(请参阅MSDN 的更多信息)

    为了您的目的,当您动态创建 5 行时,请使用 RowsAdded 事件

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        if (e.RowIndex == -1 || e.RowCount == 0)
        {
            return;
        }
    
        for (int i = 0; i < e.RowCount; i++)
        {
            var index = e.RowIndex + i;
    
            var row = DGV_ActivityDtls.Rows[index];
            row.Cells[0].Value = "Activities";
            row.Cells[1].Value = " ";
            row.Cells[2].Value = "LT";
        }
    }
    

    【讨论】:

    • 谢谢 Gzaxx ..我还有 4 行要显示.. 我怎样才能显示它们?
    • 我不明白,你想给他们看?然后更改 row.Visible = true; 如果它们被隐藏。
    • 我的意思是当我打开包含 DATAGRIDVIEW 的屏幕时,DATAGRIDVIEW 应该填充以下数据.. 活动 -- LT、燃料 -- 单位、电池缺陷 -- 单位、机械辅助 -- 单位, 起始液 -- 单位,
    • 您可以在创建这些行时用适当的数据填充它们。
    • 如何填充多行?请帮忙
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多