我要在C#窗體中創建表格。此表格必須列出日期:格里曆日期和波斯日期。 公曆日期列由來自SQL數據庫的存儲過程填充。 我有以下功能,公曆日期轉換爲波斯日期:
public string miladitoshamsi(DateTime _date)
{
PersianCalendar pc = new PersianCalendar();
StringBuilder sb = new StringBuilder();
sb.Append(pc.GetYear(_date).ToString("00"));
sb.Append("-");
sb.Append(pc.GetMonth(_date).ToString("00"));
sb.Append("-");
sb.Append(pc.GetDayOfMonth(_date).ToString("00"));
return sb.ToString();
}
我想在formload事件這段代碼填寫波斯日期列。但它仍然是空的。
for (int i = 0; i < Grd.RowCount-1 ; i++)
{
DateTime bdate = Convert.ToDateTime(Grd.Rows[i].Cells["GregorianDate"].Value);
string s = miladitoshamsi(bdate).ToString();
Grd.Rows[i].Cells[0].Value = s.ToString();
//MessageBox.Show(s);
}
當我使用messagebox它會返回正確的波斯日期。但它無法填充Data grid Persian Date列。
是字符串類型的Persion Date列嗎? –
它的列類型完全是「DataGridViewTextBoxColumn」。 – Behnam
您是否在您的Form_Load事件處理程序中放置了斷點以確保(1)它被調用,(2)Grd.RowCount不爲零? –