2017-10-04 88 views
0

フォームを使用してDataGridViewを含むプログラムを開発しています。このDataGridViewにXMLファイルからデータをインポートします。 このDataGridView内で、このデータを追加、編集、削除し、ボタンをクリックするとこの変更をXMLファイルに保存できます。 (2つの列があります) 私の問題は、ボタンがクリックされたときにセルが空であるかどうかを確認する必要があることです。その場合、これを示すMessageBoxが表示され、この変更を保存させません。DataGridViewセルが空であるかどうかを確認する方法?

私はループなどで試してみましたが、役に立たないものは見つかりませんでした。 誰かが私を助けることを願っています!テーブルセルの上に、ネストされたループを行うおかげ

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(); 
    } 

} 
} 

答えて

0

ただ、このような何か:NULL値がメッセージ・ショー(なし) を表示がある場合

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; 
} 
0
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 
      } 
    } 
} 
0

//は、例としてこれを取ります//ヌル値が存在しない場合、ファイルダイアログを開く

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; 




      } 
     } 
関連する問題