简体   繁体   中英

DataGridView How to check if a cell is empty?

I'm developing a program that contains a DataGridView whitin a form, and I'm importing data to this DataGridView from a XML file. Inside this DataGridView, I'm able to add, edit and delete this data and save this changes to the XML file when a button is clicked. (There are two columns.) My issue here is that I need to check if any cell is empty when the button is clicked, and in that case, show up a MessageBox indicating this and do not let me save this changes.

I've tried for loops and more, and couldn't find anything helpful. Hope someone can help me! Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Sullair
{
public partial class IPs : Form
{
    public IPs()
    {
        InitializeComponent();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

    }

    private void IPs_Load(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();
            ds.ReadXml(@"C:\Users\Administrador\source\repos\Sullair\schema.xml");
            dataGridView1.DataSource = ds.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void Save()
    {
        DataTable db = (DataTable)dataGridView1.DataSource;
        db.WriteXml(@"C:\Users\Administrador\source\repos\Sullair\schema.xml");
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        Save();   
    }

}
}

//take this as example when there is null value display a message show (no) //if there is not null value open file dialog

  if (string.IsNullOrEmpty(metroGrid2.CurrentRow.Cells["FileName"].Value as string))
               {
                   MessageBox.Show("no");
               }
               else
               {
                   FolderBrowserDialog fbd = new FolderBrowserDialog();
                   if (fbd.ShowDialog() == DialogResult.OK)
                   {

                       folder = fbd.SelectedPath;




           }
       }

Just do a nested loop over table cells, something like this:

private bool AreAllCellsFilled(DataTable t)
{
    if (t == null || t.Rows.Count == 0 || t.Columns.Count == 0) return true;

    for (int rowIdx = 0; rowIdx < t.Rows.Count; rowIdx++)
    {
        for (int colIdx = 0; colIdx < t.Columns.Count; colIdx++)
        {
            if (t.Rows[rowIdx][colIdx] == DBNull.Value)
            {
                MessageBox.Show($"Cell {colIdx + 1} of row {rowIdx + 1} is empty");
                return false;
            }
        }
    }

    return true;
}
foreach(DataGridViewRow row in dataGridView1.Rows)
{
    foreach(DataGridViewCell cell in row.Cells)
    {
        if(string.IsNullOrEmpty(cell.Value as string))
           {
           //cell is empty
            }
            else
             {
               //cell is not empty
            }
    }
}

This works for me. to check if specific cell is empty or null.

    if (string.IsNullOrEmpty(dataGridView_A0.Rows[0].Cells[0].Value as string))
    {
         MessageBox.Show("Null or Empty", "Results");
    }

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