Greetings from a ultimate rookie.
I'm preparing a time sheet schedule program in WinForms with C# for my geologist crew and having problem while populating PDF tables from DataGridView values.
There is a monthly schedule in my program. When you click Get Excel (I'll change it as PDF), program creates a pdf file from DataGridView values. If you got a full filled datagridview with 31 days, there is no problem. But when the month has 30 days or the user want to get PDF before month ends, it gives null exception.
Code is here.
#region Functions
DataTable MakeDataTable()
{
//Create time sheet table object
DataTable dtgdata = new DataTable();
//Define columns
dtgdata.Columns.Add("Date");
dtgdata.Columns.Add("Type of Work");
dtgdata.Columns.Add("Contract");
dtgdata.Columns.Add("Personnel ID");
#region PopulateData
string date_1 = dtgList.Rows[0].Cells[1].Value.ToString();
string date_2 = dtgList.Rows[1].Cells[1].Value.ToString();
string date_3 = dtgList.Rows[2].Cells[1].Value.ToString();
string date_4 = dtgList.Rows[3].Cells[1].Value.ToString();
string date_5 = dtgList.Rows[4].Cells[1].Value.ToString();
string date_6 = dtgList.Rows[5].Cells[1].Value.ToString();
string date_7 = dtgList.Rows[6].Cells[1].Value.ToString();
string date_8 = dtgList.Rows[7].Cells[1].Value.ToString();
string date_9 = dtgList.Rows[8].Cells[1].Value.ToString();
string date_10 = dtgList.Rows[9].Cells[1].Value.ToString();
string date_11 = dtgList.Rows[10].Cells[1].Value.ToString();
string date_12 = dtgList.Rows[11].Cells[1].Value.ToString();
string date_13 = dtgList.Rows[12].Cells[1].Value.ToString();
string date_14 = dtgList.Rows[13].Cells[1].Value.ToString();
string date_15 = dtgList.Rows[14].Cells[1].Value.ToString();
string date_16 = dtgList.Rows[15].Cells[1].Value.ToString();
string date_17 = dtgList.Rows[16].Cells[1].Value.ToString();
string date_18 = dtgList.Rows[17].Cells[1].Value.ToString();
string date_19 = dtgList.Rows[18].Cells[1].Value.ToString();
string date_20 = dtgList.Rows[19].Cells[1].Value.ToString();
string date_21 = dtgList.Rows[20].Cells[1].Value.ToString();
string date_22 = dtgList.Rows[21].Cells[1].Value.ToString();
string date_23 = dtgList.Rows[22].Cells[1].Value.ToString();
string date_24 = dtgList.Rows[23].Cells[1].Value.ToString();
string date_25 = dtgList.Rows[24].Cells[1].Value.ToString();
string date_26 = dtgList.Rows[25].Cells[1].Value.ToString();
string date_27 = dtgList.Rows[26].Cells[1].Value.ToString();
string date_28 = dtgList.Rows[27].Cells[1].Value.ToString();
string date_29 = dtgList.Rows[28].Cells[1].Value.ToString();
string date_30 = dtgList.Rows[29].Cells[1].Value.ToString();
string date_31 = dtgList.Rows[30].Cells[1].Value.ToString();
////////////////////////////////////////////////////////////
string work_1 = dtgList.Rows[0].Cells[3].Value.ToString();
string work_2 = dtgList.Rows[1].Cells[3].Value.ToString();
string work_3 = dtgList.Rows[2].Cells[3].Value.ToString();
string work_4 = dtgList.Rows[3].Cells[3].Value.ToString();
string work_5 = dtgList.Rows[4].Cells[3].Value.ToString();
string work_6 = dtgList.Rows[5].Cells[3].Value.ToString();
string work_7 = dtgList.Rows[6].Cells[3].Value.ToString();
string work_8 = dtgList.Rows[7].Cells[3].Value.ToString();
string work_9 = dtgList.Rows[8].Cells[3].Value.ToString();
string work_10 = dtgList.Rows[9].Cells[3].Value.ToString();
string work_11 = dtgList.Rows[10].Cells[3].Value.ToString();
string work_12 = dtgList.Rows[11].Cells[3].Value.ToString();
string work_13 = dtgList.Rows[12].Cells[3].Value.ToString();
string work_14 = dtgList.Rows[13].Cells[3].Value.ToString();
string work_15 = dtgList.Rows[14].Cells[3].Value.ToString();
string work_16 = dtgList.Rows[15].Cells[3].Value.ToString();
string work_17 = dtgList.Rows[16].Cells[3].Value.ToString();
string work_18 = dtgList.Rows[17].Cells[3].Value.ToString();
string work_19 = dtgList.Rows[18].Cells[3].Value.ToString();
string work_20 = dtgList.Rows[19].Cells[3].Value.ToString();
string work_21 = dtgList.Rows[20].Cells[3].Value.ToString();
string work_22 = dtgList.Rows[21].Cells[3].Value.ToString();
string work_23 = dtgList.Rows[22].Cells[3].Value.ToString();
string work_24 = dtgList.Rows[23].Cells[3].Value.ToString();
string work_25 = dtgList.Rows[24].Cells[3].Value.ToString();
string work_26 = dtgList.Rows[25].Cells[3].Value.ToString();
string work_27 = dtgList.Rows[26].Cells[3].Value.ToString();
string work_28 = dtgList.Rows[27].Cells[3].Value.ToString();
string work_29 = dtgList.Rows[28].Cells[3].Value.ToString();
string work_30 = dtgList.Rows[29].Cells[3].Value.ToString();
string work_31 = dtgList.Rows[30].Cells[3].Value.ToString();
///////////////////////////////////////////////////////////
string contract_1 = dtgList.Rows[0].Cells[4].Value.ToString();
string contract_2 = dtgList.Rows[1].Cells[4].Value.ToString();
string contract_3 = dtgList.Rows[2].Cells[4].Value.ToString();
string contract_4 = dtgList.Rows[3].Cells[4].Value.ToString();
string contract_5 = dtgList.Rows[4].Cells[4].Value.ToString();
string contract_6 = dtgList.Rows[5].Cells[4].Value.ToString();
string contract_7 = dtgList.Rows[6].Cells[4].Value.ToString();
string contract_8 = dtgList.Rows[7].Cells[4].Value.ToString();
string contract_9 = dtgList.Rows[8].Cells[4].Value.ToString();
string contract_10 = dtgList.Rows[9].Cells[4].Value.ToString();
string contract_11 = dtgList.Rows[10].Cells[4].Value.ToString();
string contract_12 = dtgList.Rows[11].Cells[4].Value.ToString();
string contract_13 = dtgList.Rows[12].Cells[4].Value.ToString();
string contract_14 = dtgList.Rows[13].Cells[4].Value.ToString();
string contract_15 = dtgList.Rows[14].Cells[4].Value.ToString();
string contract_16 = dtgList.Rows[15].Cells[4].Value.ToString();
string contract_17 = dtgList.Rows[16].Cells[4].Value.ToString();
string contract_18 = dtgList.Rows[17].Cells[4].Value.ToString();
string contract_19 = dtgList.Rows[18].Cells[4].Value.ToString();
string contract_20 = dtgList.Rows[19].Cells[4].Value.ToString();
string contract_21 = dtgList.Rows[20].Cells[4].Value.ToString();
string contract_22 = dtgList.Rows[21].Cells[4].Value.ToString();
string contract_23 = dtgList.Rows[22].Cells[4].Value.ToString();
string contract_24 = dtgList.Rows[23].Cells[4].Value.ToString();
string contract_25 = dtgList.Rows[24].Cells[4].Value.ToString();
string contract_26 = dtgList.Rows[25].Cells[4].Value.ToString();
string contract_27 = dtgList.Rows[26].Cells[4].Value.ToString();
string contract_28 = dtgList.Rows[27].Cells[4].Value.ToString();
string contract_29 = dtgList.Rows[28].Cells[4].Value.ToString();
string contract_30 = dtgList.Rows[29].Cells[4].Value.ToString();
string contract_31 = dtgList.Rows[30].Cells[4].Value.ToString();
/////////////////////////////
string p_id = txtPersonelID.Text;
//Populate with DataGridView
dtgdata.Rows.Add($"{date_1}", $"{work_1}", $"{contract_1}", $"{p_id}");
dtgdata.Rows.Add($"{date_2}", $"{work_2}", $"{contract_2}", $"{p_id}");
dtgdata.Rows.Add($"{date_3}", $"{work_3}", $"{contract_3}", $"{p_id}");
dtgdata.Rows.Add($"{date_4}", $"{work_4}", $"{contract_4}", $"{p_id}");
dtgdata.Rows.Add($"{date_5}", $"{work_5}", $"{contract_5}", $"{p_id}");
dtgdata.Rows.Add($"{date_6}", $"{work_6}", $"{contract_6}", $"{p_id}");
dtgdata.Rows.Add($"{date_7}", $"{work_7}", $"{contract_7}", $"{p_id}");
dtgdata.Rows.Add($"{date_8}", $"{work_8}", $"{contract_8}", $"{p_id}");
dtgdata.Rows.Add($"{date_9}", $"{work_9}", $"{contract_9}", $"{p_id}");
dtgdata.Rows.Add($"{date_10}", $"{work_10}", $"{contract_10}", $"{p_id}");
dtgdata.Rows.Add($"{date_11}", $"{work_11}", $"{contract_11}", $"{p_id}");
dtgdata.Rows.Add($"{date_12}", $"{work_12}", $"{contract_12}", $"{p_id}");
dtgdata.Rows.Add($"{date_13}", $"{work_13}", $"{contract_13}", $"{p_id}");
dtgdata.Rows.Add($"{date_14}", $"{work_14}", $"{contract_14}", $"{p_id}");
dtgdata.Rows.Add($"{date_15}", $"{work_15}", $"{contract_15}", $"{p_id}");
dtgdata.Rows.Add($"{date_16}", $"{work_16}", $"{contract_16}", $"{p_id}");
dtgdata.Rows.Add($"{date_17}", $"{work_17}", $"{contract_17}", $"{p_id}");
dtgdata.Rows.Add($"{date_18}", $"{work_18}", $"{contract_18}", $"{p_id}");
dtgdata.Rows.Add($"{date_19}", $"{work_19}", $"{contract_19}", $"{p_id}");
dtgdata.Rows.Add($"{date_20}", $"{work_20}", $"{contract_20}", $"{p_id}");
dtgdata.Rows.Add($"{date_21}", $"{work_21}", $"{contract_21}", $"{p_id}");
dtgdata.Rows.Add($"{date_22}", $"{work_22}", $"{contract_22}", $"{p_id}");
dtgdata.Rows.Add($"{date_23}", $"{work_23}", $"{contract_23}", $"{p_id}");
dtgdata.Rows.Add($"{date_24}", $"{work_24}", $"{contract_24}", $"{p_id}");
dtgdata.Rows.Add($"{date_25}", $"{work_25}", $"{contract_25}", $"{p_id}");
dtgdata.Rows.Add($"{date_26}", $"{work_26}", $"{contract_26}", $"{p_id}");
dtgdata.Rows.Add($"{date_27}", $"{work_27}", $"{contract_27}", $"{p_id}");
dtgdata.Rows.Add($"{date_28}", $"{work_28}", $"{contract_28}", $"{p_id}");
dtgdata.Rows.Add($"{date_29}", $"{work_29}", $"{contract_29}", $"{p_id}");
dtgdata.Rows.Add($"{date_30}", $"{work_30}", $"{contract_30}", $"{p_id}");
dtgdata.Rows.Add($"{date_31}", $"{work_31}", $"{contract_31}", $"{p_id}");
#endregion
return dtgdata;
}
#endregion
Problem is;
When the DataGridView rows are empty with some reason; code gives Null Exception.
What I want to do?
I want to check the values is it null or not, and convert them to a string (like "n/a") if they're null.
I think i need to use foreach, but I don't know how to do it.