0

我在水晶报表中创建了一个报表。它的代码可以正常工作,但我有 1 个问题。例如,如果备注字段为“A”,则应将备注颜色为“红色”,如果结果为“B”,则应将备注颜色为“蓝色”。你能帮帮我吗?这是我的代码。

SqlConnection cnn;
string connectionString = null;
string sql = null;
connectionString = "data source=Server; initial catalog=Database1;user id=sa; password= passw0rd";
cnn = new SqlConnection(connectionString);
cnn.Open();
sql = "select Date as DataColumn1, Name as DataColumn2, Year as DataColumn3 Remarks as DataColumn4 from TableStudents";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
cnn.Close();

DataSet1 ds = new DataSet1();
dscmd.Fill(ds, "DataTable1");

CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[0]);

objRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("Reports.pdf"));
ScriptManager.RegisterStartupScript(base.Page, base.Page.GetType(), "check0", "window.open('" + "Reports.pdf" + "','win2','status=no,toolbar=no,scrollbars=no,titlebar=no,menubar=no,resizable=yes,directories=no,location=no');", true);

此代码将完美地在新窗口中打开报告。只是换颜色的问题。我没有任何想法。请告诉我该怎么办。这是一个简单的概念,但我不知道下一步该做什么。谢谢。

if(result=="A")
{
result.color = "red";
}
else
{
result.color = "blue";
}
4

2 回答 2

1

我无法理解您对结果的看法,但我想您有一个字段对象,并且您想根据更改该字段对象来更改颜色,因为代码是:

            foreach (Section sec in report1.Sections)
            {
                for (int i = 1; i < sec.ReportObjects.Count + 1; i++)
                {
                    objMain = report1.Sections[sec.Name].ReportObjects[i];
                    try
                        {
                            FieldObject to1 = (FieldObject)objMain;
                            if(to1.Value == "A")
                            {                                
                                to1.TextColor = ColorToUInt(Color.Red);
                            }
                            else
                            {
                                to1.TextColor = ColorToUInt(Color.Blue);
                            }
                        }
                        catch (Exception){}
                }                
            }

最后你也必须添加这个方法:

    public uint ColorToUInt(Color color)
    {
        return (uint)((color.A << 24) | (color.R << 16) |
        (color.G << 8) | (color.B << 0));
    }
于 2013-10-06T13:57:13.677 回答
0

您是否考虑过这些链接:

动态改变字体颜色 1

动态更改字体颜色 2

于 2013-09-24T22:18:15.580 回答