在c#中开发的windows应用程序中更改gridview特定行的颜色 [英] change color of specific row of gridview in windows application developed in c#
问题描述
我遇到了一个奇怪的问题...我有一个girdview来显示库存数据。在这个股票数据中,我必须突出黄色的负面股票记录。
我做了一些谷歌搜索,并找到了一个解决方案,当我通过program.cs文件将此表单设置为默认应用程序时,该解决方案工作正常。但是每当我通过MASTER表单(即MDI Parent)调用表单时,更改行颜色的代码都不起作用。我调试了代码,发现程序正确地通过了逻辑,但没有改变行颜色。
下面是我写的代码...
I have stuck in a weird problem... I have a girdview to display stock data. In this stock data I have to highlight NEGATIVE stock records in YELLOW.
I did some google search for this and found a solution, which worked fine when I set this form as default form of application through program.cs file. But whenever I invoke the form through a MASTER form (i.e. MDI Parent), code for changing row color does not work. I debugged the code and found that program go through the logic properly but does not change row color.
Below is the code which I have written...
private void LoadData()
{
getLotID_frmLotReport.LotID__frmLotReport = 0; //Table unique ID to fetch records
string sql = "Select LotID, LotNumber as 'Lot Number', LotName as 'Lot Name', Size_ as Size, convert(varchar(10),LotCreateDate,105) as 'Create Date', round(TotalLotPurchased,2) as 'Lot Purchased', round(TotalLotSold,2) as 'Lot Sold', isnull(round(TotalLotPurchased,2),0)-isnull(round(TotalLotSold,2),0) as 'Total Stock' from dbo.Stock_Lot";
clsPurchase obj = new clsPurchase(); // Class to execute select query
DataSet ds = obj.GetData(sql); // function in above class to execute query & get data in dataset
try
{
if (ds.Tables[0].Rows.Count > 0)
{
BindingSource bsource = new BindingSource();
DataGridViewLinkColumn dl = new DataGridViewLinkColumn();
dl.DataPropertyName = "Lot Number";
dl.Name = "Lot Number";
dataGridView1.Columns.Add(dl);
//this.dataGridView1.Columns["Lot Number"].SortMode = DataGridViewColumnSortMode.Automatic;
//this.dataGridView1.Columns["Lot Name"].SortMode=DataGridViewColumnSortMode.NotSortable;
bsource.DataSource = ds.Tables[0];
dataGridView1.DataSource = bsource;
this.dataGridView1.Columns["LotID"].Visible = false;
this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 9);
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9);
//***************** NOW BELOW IS THE CODE TO CHANGE COLOR OF SPECIFIC ROWS TO YELLOW ********//
//***************** THIS CODE WORKS IF THIS FORM IS DEFAULT FORM AND NOT INVOKED THROUGH master page **************** //
string RowIndex = "";
string[] RowIndex_arr;
clsConnection objConn = new clsConnection();
string conn = objConn.ConnDB();
string sql1 = "Select isnull(round(TotalLotPurchased,1),0)-isnull(round(TotalLotSold,1),0) as 'Total Stock' from dbo.Stock_Lot";
SqlConnection connection = new SqlConnection(conn);
connection.Open();
SqlCommand cmd = new SqlCommand(sql1, connection);
SqlDataReader rd = cmd.ExecuteReader();
int i = 0;
if (rd.HasRows)
{
while (rd.Read())
{
if (Convert.ToDouble(rd["Total Stock"]) <= 0)
{
RowIndex = RowIndex + i.ToString() + ",";
}
i = i + 1;
}
}
connection.Close();
connection.Dispose();
RowIndex = RowIndex.Remove(RowIndex.Length - 1);
RowIndex_arr = RowIndex.Split(',');
foreach (string s in RowIndex_arr)
{
if (s != "")
{
this.dataGridView1.Rows[Convert.ToInt32(s)].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}
else
{
MessageBox.Show("No Data found!");
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
如果可能,请尝试通过此方式运行此代码形成默认表单以及通过MASTER页面调用,或者如果你已经开发了类似的东西,那么为我提供另一种选择。如果你对我的代码一无所知,请告诉我......
这个任务对于项目的完成是必不可少的,我已经越过了截止日期。这是应用程序的显示停止功能。
任何帮助都会非常受欢迎!
谢谢和问候,
SAM D
If possible try to run this code at your end by making this form default form as well as invoke through MASTER page, or provide me another alternative if u have already developed something like that. Please let me know if u don't understand anything from my code...
This task is imperative for the completion of project and I have already crossed a deadline. This is a show Stopper feature of application.
Any help would be GREATLY APPRECIATED!
Thanks and regards,
SAM D
推荐答案
添加dataGridView1的DataBindingComplete事件来改变颜色..
了解更多关于DataBindingComplete事件的dataGridView检查以下链接
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview。 databindingcomplete.aspx [ ^ ]
试试这段代码
add DataBindingComplete event of dataGridView1 to change color..
to know more about DataBindingComplete event of dataGridView check following link
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.databindingcomplete.aspx[^]
try this code
private void LoadData()
{
getLotID_frmLotReport.LotID__frmLotReport = 0; //Table unique ID to fetch records
string sql = "Select LotID, LotNumber as 'Lot Number', LotName as 'Lot Name', Size_ as Size, convert(varchar(10),LotCreateDate,105) as 'Create Date', round(TotalLotPurchased,2) as 'Lot Purchased', round(TotalLotSold,2) as 'Lot Sold', isnull(round(TotalLotPurchased,2),0)-isnull(round(TotalLotSold,2),0) as 'Total Stock' from dbo.Stock_Lot";
clsPurchase obj = new clsPurchase(); // Class to execute select query
DataSet ds = obj.GetData(sql); // function in above class to execute query & get data in dataset
try
{
if (ds.Tables[0].Rows.Count > 0)
{
BindingSource bsource = new BindingSource();
DataGridViewLinkColumn dl = new DataGridViewLinkColumn();
dl.DataPropertyName = "Lot Number";
dl.Name = "Lot Number";
dataGridView1.Columns.Add(dl);
//this.dataGridView1.Columns["Lot Number"].SortMode = DataGridViewColumnSortMode.Automatic;
//this.dataGridView1.Columns["Lot Name"].SortMode=DataGridViewColumnSortMode.NotSortable;
bsource.DataSource = ds.Tables[0];
dataGridView1.DataSource = bsource;
this.dataGridView1.Columns["LotID"].Visible = false;
this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 9);
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
DataGridViewCellStyle myColor= dataGridView1.DefaultCellStyle.Clone();
myColor.BackColor = Color.Yellow;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
if (row.Cells["Total Stock"].Value.ToString() != "")
{
row.DefaultCellStyle = myColor;
}
}
}
}
45452
}
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
DataGridViewCellStyle myColor= dataGridView1.DefaultCellStyle.Clone();
myColor.BackColor = Color.Yellow;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
这篇关于在c#中开发的windows应用程序中更改gridview特定行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!