如何将 gridview 单元格中的空值添加到列表中?
How to add empty value from gridview cell to list?
我想在 list<string>
中输入客人详细信息(头衔、名字、中间名、姓氏),客人详细信息可以 empty.I 使用 LINQ 插入 list.I已针对 LINQ 代码提及此问题 DataGridView write all values from a column to list
我只想将文本输入列表(如果有)或插入空字符串(如果没有)have.Right 现在如果我将文本框留空,它将抛出 object reference not set to instance of an object exception
private string SaveGuestDetails()
{
string strRet = string.Empty;
Reservation obj = new Reservation();
FinalReservationDetails f = new FinalReservationDetails();
try
{
//int i = dgvTextBoxes.Rows.Count;
List<DataRow> personsList = new List<DataRow>();
int j = 0;
for (int i = 0; i < dgvTextBoxes.Rows.Count; i++)
{
f.SubSrNo = j + 1;
f.GuestTitle = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtTitle"].Value.ToString())
.ToList();
f.FirstName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtFname"].Value.ToString())
.ToList();
f.MidName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtMName"].Value.ToString())
.ToList();
f.LastName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtLname"].Value.ToString())
.ToList();
}
}
catch (Exception ex)
{
}
return strRet;
}
您的问题是您的单元格值可以是字符串、DBNull.Value 或空值。
null.ToString()
抛出 object reference not set to instance of an object exception
。为避免这种情况,您可以先设置对象 as string
以摆脱 DBNull.Value,然后使用 null-coalescing operator 将 null 设置为空字符串。
object unknown = System.DBNull.Value;
string converted1 = (unknown as string) ?? string.Empty;
// returns string.Empty
unknown = null;
string converted2 = (unknown as string) ?? string.Empty;
// returns string.Empty
unknown = "text";
string converted3 = (unknown as string) ?? string.Empty;
// returns text
你的情况:
f.GuestTitle = dgvTextBoxes.Rows.OfType<DataGridViewRow>().Select
(r => (r.Cells["txtTitle"].Value as string) ?? string.Empty ).ToList();
我想在 list<string>
中输入客人详细信息(头衔、名字、中间名、姓氏),客人详细信息可以 empty.I 使用 LINQ 插入 list.I已针对 LINQ 代码提及此问题 DataGridView write all values from a column to list
我只想将文本输入列表(如果有)或插入空字符串(如果没有)have.Right 现在如果我将文本框留空,它将抛出 object reference not set to instance of an object exception
private string SaveGuestDetails()
{
string strRet = string.Empty;
Reservation obj = new Reservation();
FinalReservationDetails f = new FinalReservationDetails();
try
{
//int i = dgvTextBoxes.Rows.Count;
List<DataRow> personsList = new List<DataRow>();
int j = 0;
for (int i = 0; i < dgvTextBoxes.Rows.Count; i++)
{
f.SubSrNo = j + 1;
f.GuestTitle = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtTitle"].Value.ToString())
.ToList();
f.FirstName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtFname"].Value.ToString())
.ToList();
f.MidName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtMName"].Value.ToString())
.ToList();
f.LastName = dgvTextBoxes.Rows
.OfType<DataGridViewRow>()
.Select(r => r.Cells["txtLname"].Value.ToString())
.ToList();
}
}
catch (Exception ex)
{
}
return strRet;
}
您的问题是您的单元格值可以是字符串、DBNull.Value 或空值。
null.ToString()
抛出 object reference not set to instance of an object exception
。为避免这种情况,您可以先设置对象 as string
以摆脱 DBNull.Value,然后使用 null-coalescing operator 将 null 设置为空字符串。
object unknown = System.DBNull.Value;
string converted1 = (unknown as string) ?? string.Empty;
// returns string.Empty
unknown = null;
string converted2 = (unknown as string) ?? string.Empty;
// returns string.Empty
unknown = "text";
string converted3 = (unknown as string) ?? string.Empty;
// returns text
你的情况:
f.GuestTitle = dgvTextBoxes.Rows.OfType<DataGridViewRow>().Select
(r => (r.Cells["txtTitle"].Value as string) ?? string.Empty ).ToList();