DataGrid1_ItemDataBound事件的相应过程中,有一下一段代码会反复出现
if (e.Item.Cells[10].Text == String.Empty || e.Item.Cells[10].Text.Equals(null) || e.Item.Cells[10].Text==" ")
{}
else
{
if (e.Item.Cells[10].Text.Substring(0,1)=="#")
{
e.Item.Cells[10].BackColor=Color.Blue;
}
else
{
dt=Convert.ToDateTime(e.Item.Cells[10].Text.ToString());
if (dt > DateTime.Today.Date) //未来
{
e.Item.Cells[10].BackColor = Color.LimeGreen;
e.Item.Cells[10].ForeColor = Color.White;
}
else //曾经
{
e.Item.Cells[10].BackColor = Color.Green;
e.Item.Cells[10].ForeColor = Color.Wheat;
}
}
}
[10]可能会从[10]到[18],我想应该用一段函数来实现.于是,构想了一个函数PaintColor(int i,object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    //.....
}然后通过PaintColor(10);来调用.这时编译时出问题,"重载PaintColor方法未获取1参数"...请各位大侠帮助!!

解决方案 »

  1.   

    PaintColor(int iItemIndex,int iCellIndex)
    {
    if (DataGrid1.Items[i].Cells[10].Text.Substring(0,1)=="#")
    {
    DataGrid1.Items[i].Cells[10].BackColor=Color.Blue;
    }
    else
    {
    dt=Convert.ToDateTime(DataGrid1.Items[i].Cells[10].Text.ToString());
    if (dt > DateTime.Today.Date) //未来
    {
    DataGrid1.Items[i].Cells[10].BackColor = Color.LimeGreen;
    DataGrid1.Items[i].Cells[10].ForeColor = Color.White;
    }
    else //曾经
    {
    DataGrid1.Items[i].Cells[10].BackColor = Color.Green;
    DataGrid1.Items[i].Cells[10].ForeColor = Color.Wheat;
    }
    }
    }
      

  2.   

    上面写错了
    i应该是iItemIndex
    10应该是iCellIndex
      

  3.   

    谢谢ncjcz(新手上路).不好意思刚才忘记说了,函数PaintColor()我是准备放在DataGrid1_ItemDataBound时间里来调用的,而且,[10]似乎不应该出现在函数里面吧...
      

  4.   

    PaintColor(int i, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        //.....
    }
    调用
            protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                for (int i = 0; i < e.Item.Cells.Count; i++)
                {
                    PaintColor(i, e);
                }
            }
      

  5.   

    谢谢各位,可还是没有解决.
    可能是我没描述清楚,PaintColor函数是要放在
    DataGrid1_ItemDataBound事件中的,那似乎是一个循环,遍历了所有的row,因此不用指明是哪个item,只需指明cell即可.private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    //------------各Column中cell涂色开始----------------------
    PaintColor(4,?); if (e.Item.Cells[4].Text == String.Empty || e.Item.Cells[4].Text.Equals(null) || e.Item.Cells[4].Text=="&nbsp;")
    {}
    else
    {
    dt=Convert.ToDateTime(e.Item.Cells[4].Text.ToString());
    if (dt > DateTime.Today.Date) //未来
    {
    e.Item.Cells[4].BackColor = Color.LimeGreen;
    e.Item.Cells[4].ForeColor = Color.White;
    }
    else //曾经
    {
    e.Item.Cells[4].BackColor = Color.Green;
    e.Item.Cells[4].ForeColor = Color.Wheat;
    }
    }

    PaintColor(5,?);
    PaintColor(6,?);
    PaintColor(7,?);
    PaintColor(8,?);

    }
    }不知这条思路对不对
      

  6.   

    还是没有明白你的意思,DataGrid1_ItemDataBound 中的e本身就是一行数据所以我上边的程序没有任何问题啊,你想把里边的内容做成一个方法,你肯定要穿一个e参数过去的这个是在进行行数据帮定时把这个一个数据行传给方法,让后方法把各个cell进行涂色,思路很正确,程序也没错阿。你实践一下吧PaintColor(int i, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        //.....
    }
    调用
            protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                for (int i = 0; i < e.Item.Cells.Count; i++)
                {
                    PaintColor(i, e);
                }
            }