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