[英]Compare datagridview cell value with image in the DataGridViewImageColumn
[英]compare two dataGridView cell with date value?
在我的C#Windows窗體中,我有一個dataGridView可以從表中加載數據,
dataGridView
有8列 ,其中兩列的日期值為borrow_date
和return_date
。
如果任何return_date
單元格的日期都大於return_date
單元格的日期,我想比較這兩列的值, borrow_date
單元格的背景設置為紅色或將其顏色設置為紅色。
下面的代碼將幫助您
<asp:GridView ID="ctlGridView" runat="server" OnRowCreated="OnRowCreated" />
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
Object ob = drv["return_date"];
Object ob1 = drv["borrow_date"];
if (!Convert.IsDBNull(ob) && !Convert.IsDBNull(ob1) )
{
DateTime date1return_date;
DateTime date2borrow_date;
if (DateTime.TryParse(ob.ToString(), out date1return_date) &&
DateTime.TryParse(ob1.ToString(), out date2borrow_date))
{
if (date1return_date > date2borrow_date)
{
TableCell cell = e.Row.Cells[1]; // Get your required cell
cell.BackColor = System.Drawing.Color.Red;
}
}
}
}
使用DateTime
比較日期。
您可以通過不同的方式來做到這一點:
1加載所有數據后循環遍歷整個grid
foreach (DataGridViewRow row in grid.Rows)
if (Convert.ToInt32(row.Cells["borrow_date"].Value) < Convert.ToInt32(row.Cells["return_date"].Value))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
2在加載每一行時執行此操作。
private void gridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (Convert.ToInt32(row.Cells["borrow_date"].Value) < Convert.ToInt32(row.Cells["return_date"].Value))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.