2
我想在状态列中显示一个圆圈,并且不绑定到GridView中的任何字段。所以如果父母ID = 1,那么我想用绿色填充圆圈,如果父母ID = 2,那么我想用黄色填充圆圈等等。状态栏应该只有基于我提到的条件的彩色圆圈。此外,圈子可能是某种形象或不确定什么是最好的和最简单的方式。这是我想要修改的代码。后面感谢如何使用C#基于单元格项目更改gridview单元格颜色
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
ondatabound="GridView1_DataBound" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="ParentID" HeaderText="ParentID"
SortExpression="ParentID" />
<asp:BoundField DataField="Content" HeaderText="Content"
SortExpression="Content" />
<asp:BoundField DataField="ShortContent" HeaderText="ShortContent"
SortExpression="ShortContent" />
<asp:TemplateField HeaderText="Status" ControlStyle-Width="75px" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BindData();
}
}
public void BindData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Title, Location, ParentID, Content from MyTable", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var item = e.Row.DataItem as DataTable;
var panel = (Panel)e.Row.FindControl("CirclePanel");
if (item.ParentID == "1")
{
panel.CssClass = "yellow";
}
else
{
panel.CssClass = "green";
}
}
}
CSS样式
<style type="text/css">
.green, .yellow { border-radius: 10px; width: 20px; height: 20px;}
.green, .yellow { -moz-border-radius: 10px; width: 20px; height: 20px;}
.green, .yellow { -webkit-border-radius: 10px; width: 20px; height: 20px;}
.green, .yellow { -moz-border-radius: 10px; width: 20px; height: 20px;}
.green { background: green; }
.yellow { background: yellow; }
</style>
谢谢赢了,但我得到一个错误,它说“作为SomeObject”。我不确定“SomeObject”是什么意思。请halp – moe 2013-05-07 14:14:41
你想转换为适当的对象。如果'DataSource'是'DataSet'或'DataTable',它将作为DataRowView的'e.Item.DataItem'。 http://stackoverflow.com/a/9667262/296861 – Win 2013-05-07 14:19:36
我更新了你的建议背后的代码,但它不喜欢它说如果(item.ParentID == 1);具体而言,它不喜欢ParentID。请看看我上面的更新后的代码。谢谢 – moe 2013-05-07 16:28:39