我有这个gridview一起显示两个表(使用merge方法)。我想更改第0行和第11行的行颜色。我知道我可以使用行数据绑定事件来执行此操作,但我不知道如何在运行时选择行。
我的代码隐藏。
public void mergetbl(DataTable DocVisTbl, DataTable cobtable)
{
DataTable mergetable = DocVisTbl.Copy();
mergetable.Merge(cobtable);
DataRow row;
row = mergetable.NewRow();
row["Stakeholder"] = "Número de Médicos";
mergetable.Rows.InsertAt(row, 0);
row = mergetable.NewRow();
row["Stakeholder"] = "Médicos Visitados";
mergetable.Rows.InsertAt(row, 11);
DataTable mergetable_Clone = mergetable.Clone(); //just copy structure, no data
for (int i = 0; i < mergetable_Clone.Columns.Count; i++)
{
if (mergetable_Clone.Columns[i].DataType != typeof(string))
mergetable_Clone.Columns[i].DataType = typeof(string);
}
foreach (DataRow datarow in mergetable.Rows)
{
mergetable_Clone.ImportRow(datarow);
}
for (int x = 1; x < mergetable_Clone.Columns.Count; x++)
{
mergetable_Clone.Rows[4][x] = mergetable_Clone.Rows[4][x] + "%";
mergetable_Clone.Rows[7][x] = mergetable_Clone.Rows[7][x] + "%";
mergetable_Clone.Rows[10][x] = mergetable_Clone.Rows[10][x] + "%";
mergetable_Clone.Rows[15][x] = mergetable_Clone.Rows[15][x] + "%";
mergetable_Clone.Rows[18][x] = mergetable_Clone.Rows[18][x] + "%";
mergetable_Clone.Rows[21][x] = mergetable_Clone.Rows[21][x] + "%";
}
MergeGrid.DataSource = mergetable_Clone;
MergeGrid.DataBind();
}
protected void MergeGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
//
//}
}
我的CSS代码
.Grid {
width: auto;
background-color: #fff;
margin: 5px 0 10px 0;
border: solid 2px #525252;
border-collapse:collapse; }
/*Gridview table data*/
.Grid td {
padding: 2px;
font-family: Calibri;
border: solid 1px #c1c1c1;
color: #000000;
text-align:right;
padding: 2px 4px 3px 4px;}
/*Gridview table header*/
.Grid th {
padding: 4px 2px;
color: #fff;
font-family:Calibri;
background: #424242 url(Images/gridheader.png) repeat-x top;
border-left: solid 1px #525252;
font-size: 95%; }
/*Gridview alternate rows*/
.Grid .alt { background: #fcfcfc url(Images/grid-alt.png) repeat-x top; }
答案 0 :(得分:2)
代码中的一个例子
protected void MergeGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0) //Select the row
{
e.Row.BackColor = System.Drawing.Color.FromArgb(255, 0, 0);
//or you can select the color
//e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
我希望能帮助你。
答案 1 :(得分:1)
首先,您可以选择或有另一种方法来实现此目的,这只是一种方法,也许您可以基于此编写另一种解决方案:
protected void MergeGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//here you should have a way to identify which rows are you goingo to change
//I used the text in the first row
string[] rowsToColor = new[] { "Número de Médicos", "Médicos Visitados" };
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (rowsToColor.Contains(e.Row.Cells[0].Text))
e.Row.BackColor = System.Drawing.Color.Blue; //it is just an example color
}
}
答案 2 :(得分:1)
在DataBind
之后执行此操作。
MergeGrid.DataBind();
MergeGrid.Rows[0].BackColor =
gvwSearch.Rows[11].BackColor =
System.Drawing.Color.Red;
无需RowDataBound
事件。
答案 3 :(得分:1)
你快到了,然后你就停了。
这两种方法都可行。
使用行索引:
private const int ROW_00 = 0;
private const int ROW_11 = 11;
protected void MergeGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var index = e.Row.RowIndex;
if ((index == ROW_00) || (index == ROW_11))
{
e.Row.BackColor = System.Drawing.Color.Aqua;
}
}
}
使用行的文本(用我的西班牙语替换我的英文版本):
private const string NUMERO_DE_MEDICOS = "Numero de Medicos";
private const string MEDICOS_VISITADOS = "Medicos Visitados";
protected void MergeGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var cell0 = e.Row.Cells[0].Text;
if ((cell0 == NUMERO_DE_MEDICOS) || (cell0 == MEDICOS_VISITADOS))
{
e.Row.BackColor = System.Drawing.Color.Aqua;
}
}
}