1

RowDataBound にこのコードがあり、セルの色を交互に変更し、セルに onclick イベントを追加します。データはストアド プロシージャから動的に取得されるため、グリッドビューにはコントロールが 1 つしかありません。ボタンフィールド。内部にデータがないセルを白色にしたいのですが、どうすればこれを実現できますか?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{


    {
        string back1 = "url('Images/GridBack1.jpg')";
        string back2 = "url('Images/GridBack2.jpg')";

        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            TableCell Cell = e.Row.Cells[i];


            // if both row and column are odd, color them black
            // if both row and column are even, color them white
            if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) ||
                ((e.Row.RowIndex % 2 == 0) && (i % 2 == 0)))


            {
                if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit
                {
                    e.Row.Cells[i].BackColor = Color.Blue;
                    e.Row.Cells[i].ForeColor = Color.White;
                }
                else
                {

                    e.Row.Cells[i].Width = Unit.Pixel(450);
                    e.Row.Cells[i].Height = Unit.Pixel(67);
                    e.Row.Cells[i].Style.Add("background-image", back1);
                    //e.Row.Cells[i].Style.Add("width", "150px");
                    //e.Row.Cells[i].Style.Add("word-wrap","break-word");
                    e.Row.Cells[i].Attributes.Add("align", "center");
                }



            }
            else
            {

                if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit
                {
                    e.Row.Cells[i].BackColor = Color.Blue;
                    e.Row.Cells[i].ForeColor = Color.White;
                }
                else
                {
                    e.Row.Cells[i].Width = Unit.Pixel(450);
                    e.Row.Cells[i].Height = Unit.Pixel(67);
                    e.Row.Cells[i].Style.Add("background-image", back2);
                    //e.Row.Cells[i].Style.Add("width", "150px");
                    //e.Row.Cells[i].Style.Add("word-wrap", "break-word");
                    e.Row.Cells[i].Attributes.Add("align", "center");
                }


            }
        string color = "#000000";
        e.Row.Cells[0].Attributes.Add("Style", "background-color: " + color + ";");
        e.Row.Cells[0].Width = Unit.Pixel(150);
        e.Row.Cells[0].Height = Unit.Pixel(67);


    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
            string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
            // Add events to each editable cell
            for (int columnIndex = 2; columnIndex < e.Row.Cells.Count; columnIndex++)
            {
                // Add the column index as the event argument parameter
                string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
                // Add this javascript to the onclick Attribute of the cell
                e.Row.Cells[columnIndex].Attributes["onclick"] = js;
                // Add a cursor style to the cells
                e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
            }
        }
    }


}
4

2 に答える 2

3

すべてのセルに白を割り当ててから、条件に応じて上書きしてみてください。

于 2013-05-28T10:36:52.597 に答える
1

string.IsNullOrEmpty()次のように使用する必要があります

if(string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
  e.Row.Cells[i].BackColor = Color.Blue;
  e.Row.Cells[i].ForeColor = Color.White;
}

MSDN

それがうまくいくことを願っています。

于 2013-05-28T08:12:02.463 に答える