简体   繁体   English

如何使用C#检查dataGridView中选定行中的单元格是否为空/空?

[英]How to check if a cell in selected row in dataGridView is empty/null, using C#?

I'm trying to write a code, which checks if a cell in selected row in dataGridView is empty/null before the cell will be updated with a value.我正在尝试编写一个代码,该代码在使用值更新单元格之前检查 dataGridView 中选定行中的单元格是否为空/空。 The code, which I wrote, works but no matter if there is a value in cell in selected row or not, the data are not updated.我编写的代码有效,但无论所选行的单元格中是否有值,数据都不会更新。 I have tried with this code:我试过这个代码:

 if (dataGridView1.SelectedRows[0].Cells[1].Value == null)
            {
                try
                {
                    String ConnectionString = @"Data Source=.\SQLEXPRESS01;Initial Catalog=Vagtplan;Integrated Security=True";
                    SqlConnection myconnection = new SqlConnection(ConnectionString);
                    myconnection.Open();
                    DateTime primaryKey = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[0].Value);
                    SqlCommand AddNumbeCommand = myconnection.CreateCommand();
                    AddNumbeCommand.CommandText = "UPDATE dbo.Vagter SET [ansatID] = @ansatID WHERE [Dato] = @dato";
                    AddNumbeCommand.Parameters.Add("@ansatID", SqlDbType.Int).Value = textBox1.Text;
                    AddNumbeCommand.Parameters.Add("@dato", SqlDbType.DateTime).Value = primaryKey;
                    AddNumbeCommand.ExecuteNonQuery();
                    myconnection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    MessageBox.Show("The cell is updated.");
                }

            }
            else
            {
                MessageBox.Show("There is already a value in the cell.");
            }

As I mentioned befor, the actual result is that no matter if there is a value in cell in selected row or not, the data are not updated.正如我之前提到的,实际结果是无论所选行的单元格中有没有值,数据都不会更新。 The expected result is that when a user select in dataGrdView a row where cell under column ansatID already has a value, write a value in textBox1, and press on ''Tilføj ansatID til vagten'', the he get error:"There is already a value in the cell.".预期的结果是,当用户在 dataGrdView 中选择 ansatID 列下的单元格已经有值的行,在 textBox1 中写入一个值,然后按下 ''Tilføj ansatID til vagten'',他得到错误:“已经有单元格中的一个值。”。 If he will select a row where cell under column ansatID is empty, write a value in textBox1, and press on ''Tilføj ansatID til vagten'', then the SQL query will be executed and he gets message "The cell is updated."如果他将选择 ansatID 列下的单元格为空的行,在 textBox1 中写入一个值,然后按“Tilføj ansatID til vagten”,则将执行 SQL 查询并收到消息“单元格已更新”。 This is also shown on following picture:这也显示在下图: 在此处输入图片说明

With this condition you can access that specific column in the selected row, you missed the OwningRow part and you need to compare to an empty string and null just in case:在这种情况下,您可以访问所选行中的特定列,但您错过了 OwningRow 部分,您需要与空字符串和 null 进行比较,以防万一:

(string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value == "" ||
(string)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value == null

SelectedCells[0].OwningRow means the first selected row, Cells[1] means is the ansatID. SelectedCells[0].OwningRow 表示第一个选中的行,Cells[1] 表示是 ansatID。

Gotta love winforms and its clarity.必须喜欢 winforms 及其清晰度。

Edit: As someone pointed out, you need to manually update the data in the datagrid in winforms, it is convinient to have a function like UpdateDatagrid() and call it every time you modify the DB.编辑:正如有人指出的那样,您需要在winforms中手动更新数据网格中的数据,有一个像UpdateDatagrid()这样的函数并在每次修改数据库时调用它是很方便的。

您可以使用String.IsNullOrWhiteSpace((String)dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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