簡體   English   中英

C#Winforms中的DataGridView問題

[英]Problems with DataGridView in C# Winforms

我在使用DataGridView時遇到問題。

首先,如您所見:
我正在將信息從DataGridView傳輸到Info_Goods數組。 我仔細檢查了一下,它完全轉移了我需要的所有信息。

但是,當我使用foreachInfo_Goods數組的信息寫入text.txt ,只會寫入第一行的信息。 我創建了一個不同的數組,名稱testArray之前創建了一些元素

例:

string[,] testArray = {{a,b,c}, {d,e,f}};

它完全寫入了testArray的信息。 我不知道發生了什么

string Name = "";
Name = tb_Name.Text;

string[,] Info_Goods = new string[50, 50];

int Number = 1;

for (int i = 0; i < dgv_Input.Rows.Count - 1; i++)
{
  for (int j = 0; j < dgv_Input.Columns.Count - 1; j++)
  {
    Info_Goods[i, j] = dgv_Input.Rows[i].Cells[j].Value.ToString();
  }
}

////================================ Write File 
 // string[,] test = { { "a", "b", "c" }, { "d", "e", "f" } };  // it worked
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Number.ToString() + " " + Name + ".txt"))
{
  foreach (string s in Info_Goods)      // it didn't worked
  {
    sw.WriteLine(s);
  }
}

其次,我想檢查DataGridView中的空元素並顯示錯誤通知。

如您所見,我使用的循環與將信息從DataGridView傳輸到Info_Goods array時使用的循環相同。

我使用了1個檢查變量(1:空元素,0:不空)。 沒用

但是,當我只編寫IF語句(不使用循環)時,它起作用了。

int check = 0;

for (int a = 0; a < dgv_Input.Rows.Count - 1; a++)
{
  for (int b = 0; b < dgv_Input.Columns.Count - 1; b++)
  {
    if (string.IsNullOrEmpty(dgv_Input.Rows[a].Cells[b].Value as string))   // it didn't work
    {
      check = 1;
    }
  }
}
// if (string.IsNullOrEmpty(dgv_Input.Rows[0].Cells[0].Value as string)) // it worked

您應該按如下所示修改循環。 然后它會正常運行,我的朋友。

for (int i = 0; i < dgv_Input.Rows.Count; i++)
{
  for (int j = 0; j < dgv_Input.Columns.Count; j++)
  {
    Info_Goods[i, j] = dgv_Input.Rows[i].Cells[j].Value.ToString();
  }
}

對於您的第一個代碼,目前尚不清楚為什么要使用[50,50]的字符串數組? DataGridView dgv_Input似乎是調整數組大小的更好方法。 還是最好使用列表。 使用數組的方式將為數組中的許多元素創建空值,否則可能會產生溢出。 假設dgv_Input有4列和5行數據,在將單元格讀入代碼后,string [50,50]數組如下所示:

[0,0] data0 col1
[0,1] data0 col2
[0,2] data0 col3
[0,3] data0 col4
[0,4] null
[0,5] null
[0,6] null
   …….
[0,47] null
[0,48] null
[0,49] null
[1,0] data1 col1
[1,1] data1 col2
[1,2] data1 col3
[1,3] data1 col4
[1,4] null
[1,5] null
[1,6] null

顯然,這浪費了很多空間,並且保證了空值/字符串。 當您嘗試將此數組寫入文件時,應使用以下命令檢查這些空值:

If (s != null)…

您可能需要重新考慮如何從DataGridView存儲字符串...我猜每行有1個字符串,可能將它們存儲在List因為您可能不知道這里有多少個字符串。

對於您的第二個問題,我想問題在於以下代碼行:

string.IsNullOrEmpty(dgv_Input.Rows[a].Cells[b].Value as string)

我認為這並沒有返回您的期望。 首先, dgv_Input.Rows[a].Cells[b].Value可能為null。 我知道該行似乎在詢問是否為空值IsNullOrEmpty的STRING。 如果DataGridView單元格中的值是數字,則上面的行也將返回true。 換句話說,上面的行似乎沒有按預期方式工作。 在下面,我細分了代碼以檢查哪些值是null或哪些值是空字符串。 這似乎按預期工作

for (int a = 0; a < dataGridView1.Rows.Count -1 ; a++) {
  for (int b = 0; b < dataGridView1.Columns.Count; b++) {
    if (dataGridView1.Rows[a].Cells[b].Value != null) {
      if (dataGridView1.Rows[a].Cells[b].Value.ToString() != "") {
        textBox1.Text += dataGridView1.Rows[a].Cells[b].Value.ToString() + ",";
      } else {
       // MessageBox.Show("String is empty: ");
      }
    } else {
      //MessageBox.Show("DGV Cell Value is null: ");
    }
  }
  textBox1.Text += Environment.NewLine;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM