我是C#的新手,这是我关于C#语言的第一个项目,我的后端是Mysql,我被困在了一个特定的点上,我有一个包含与数据库值绑定的数据视图的表单,因为我有一个列" TNS_Date“--它是产品的终止日期,我希望如果TNS_Date大于当前日期,那么特定单元的背景颜色应该在colured中得到红色,我怎么能这样做,请帮我做这个。
我正在使用winforms,直到现在我还在尝试这个
foreach (DataGridViewRow row in TNSFormdgv.Rows)
{
var now = DateTime.Now;
var expirationDate = DateTime.Parse(row.Cells[15].Value.ToString());
var OneMonthBefore = expirationDate.AddDays(-30);
if (now > OneMonthBefore && now < expirationDate)
{
row.DefaultCellStyle.BackColor = Color.Yellow;
}
else if (now > expirationDate)
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}当我执行项目时,我得到了这个错误
Object reference not set to an instance of an object这是我那份表格的全部代码,请检查我出错的地方。
public partial class TNS_Subscription : Form
{
public TNS_Subscription()
{
InitializeComponent();
TNSSubscriptionForm();
}
private void TNSFormBackbtn_Click(object sender, EventArgs e)
{
new Form2().Show();
this.Hide();
}
public void TNSSubscriptionForm()
{
string ConString = "datasource=localhost;port=3306;username=root;password=ajay";
MySqlConnection conDataBase = new MySqlConnection(ConString);
MySqlCommand cmdDatabase = new MySqlCommand("Select acmecmpdetails.Cmp_Number,acmecmpdetails.Cmp_Name,acmecmpdetails.Cmp_AdminId,acmecmpdetails.Cmp_Address1,acmecmpdetails.Cmp_Country1,acmecmpdetails.Cmp_State1,acmecmpdetails.Cmp_City1,acmecmpdetails.Cmp_PostalCode,acmecmpdetails.Contact_Person1,acmecmpdetails.LandLine_Number1,acmecmpdetails.MobileNumber,acmecmpdetails.Cntct_Emailid,acmetally_detail.TallySerial_No,acmetally_detail.Tally_Release,acmetally_detail.Tally_Edition,acmetally_detail.TNS_Date,acmetally_detail.Tally_PrefPartner,acmetally_detail.Tally_accountId from acmesolutionsdatabase.acmecmpdetails INNER JOIN acmesolutionsdatabase.acmetally_detail ON acmesolutionsdatabase.acmecmpdetails.TallySerial_No= acmesolutionsdatabase.acmetally_detail.TallySerial_No;", conDataBase);
try
{
MySqlDataAdapter cddsda = new MySqlDataAdapter();
cddsda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
cddsda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
TNSFormdgv.DataSource = bsource;
cddsda.Update(dbdataset);
for(int i=0;i<TNSFormdgv.Rows.Count;i++)
{
if (Convert.ToDateTime(TNSFormdgv.Rows[i].Cells["TNS_Date"].Value.ToString()) > System.DateTime.Now.Date)
{
TNSFormdgv.Rows[i].Cells["TNS_Date"].Style.BackColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}提前谢谢
发布于 2014-03-06 06:31:43
您可以简单地这样做:
for(int i=0;i<MyGridView.Rows.Count;i++)
{
if (Convert.ToDateTime(MyGridView.Rows[i].Cells["TNS_Date"].Value.ToString()) > System.DateTime.Now.Date)
{
MyGridView.Rows[i].Cells["TNS_Date"].Style.BackColor = System.Drawing.Color.Red;
}
}如果它的值大于当前日期,它将更改单元格"TNS_Date“的背景色。
或您可以使用列TNS_Date的索引如下:
如果列TNS_Date的索引为3,那么:
for(int i=0;i<MyGridView.Rows.Count;i++)
{
if (Convert.ToDateTime(MyGridView.Rows[i].Cells[3].Value.ToString()) > System.DateTime.Now.Date)
{
MyGridView.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
}
}我希望它能解决这个问题。
发布于 2014-03-06 06:29:42
以下是一些可能有所帮助的示例:
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.DataSourceChanged += dataGridView1_DataSourceChanged;
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass { ID = 1, Name = "1", Date = DateTime.Now });
list.Add(new MyClass { ID = 2, Name = "2", Date = DateTime.Now });
list.Add(new MyClass { ID = 3, Name = "3", Date = DateTime.Now });
list.Add(new MyClass { ID = 4, Name = "4", Date = DateTime.Now.AddDays(1) });
dataGridView1.DataSource = list;
}
void dataGridView1_DataSourceChanged(object sender, EventArgs e)
{
// Loop through all the cells where date is in future
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Cells[2].Value != null) && (DateTime)row.Cells[2].Value > DateTime.Now)
{
row.Cells[2].Style.BackColor = Color.Blue;
}
}
}发布于 2014-03-06 06:45:12
我假设它在Windows窗体中,因为您使用的是datagridview。
对于datagridview,您需要遍历网格的每个单元格,以找到正确的单元格并设置背景颜色。
为此,我创建了以下代码。
void loadGrid()
{
DataTable dtData = new DataTable();
dtData.Columns.Add("TNS_Date");
DataRow dRow;
clmDate.DataPropertyName = "TNS_Date";
dRow= dtData.NewRow();
dRow["TNS_Date"] = "20-01-2014";
dtData.Rows.Add(dRow);
dRow = dtData.NewRow();
dRow["TNS_Date"] = "20-02-2014";
dtData.Rows.Add(dRow);
dRow = dtData.NewRow();
dRow["TNS_Date"] = "20-03-2014";
dtData.Rows.Add(dRow);
dgvGrid.DataSource = dtData;
for (int i = 0; i <= dgvGrid.RowCount - 1; ++i)
{
//Finding the right cell to change colour.
if(DateTime.Parse(dgvGrid["TNS_Date", i].Value.ToString()) >= DateTime.Now.Date)
dgvGrid[0, i].Style.BackColor = Color.Bisque;
}}
https://stackoverflow.com/questions/22216685
复制相似问题