简体   繁体   中英

How to DIsable Button When DataGridView Data Source is empty? C#

I have DataGridView with a empty DataTable as DataSource . When the main form loads it will show an empty DataGridView so user can entry data on it. I want to disable the Save Data menu when DataGridView is empty. I tried this code :

private void dGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            DataTable dt = dGV.DataSource as DataTable;
            int d = dt.Columns.Count + 1;
            foreach (var column in dt.Columns.Cast<DataColumn>().ToArray())
            {
                if (dt.AsEnumerable().All(dr => dr.IsNull(column)))
                    d--;
            }
            if (d > 0)
            {
                saveDataToolStripMenuItem.Enabled = true;
            }
            else {
                saveDataToolStripMenuItem.Enabled = false;
            }
        }

The Save Data menu will be disabled when form loads

在此处输入图片说明

and it will be enabled when I entry a value on the dGV . 在此处输入图片说明

But the Save Data menu wont be disabled when I delete that value and the dGV becomes empty again.

在此处输入图片说明

Any suggestion?

1- When the form loads, check If dGV is empty, then set the menu button disabled.

2- When creating a new entry, make button enabled.

3- When Remove button is clicked. Check if dGV is empty or have records, then again disable the menuitem if it is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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