如何将样式仅应用于datagrid中的一个单元格? [英] how to apply style to only one cell in datagrid?

查看:78
本文介绍了如何将样式仅应用于datagrid中的一个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在DataGrid中显示数据.
在该DataGrid中有5列.它们是EmpID,Ename,Esal,EDept,Location.

我只想更改Ename单元格.
我想给背景和边框加上颜色并应用边框样式.

我该怎么办?
请给我一个解决方案.

谢谢

Hi,

I am displaying data in DataGrid.
In that DataGrid there are 5 columns. They are EmpID, Ename, Esal, EDept, Location.

I want to change only Ename cell.
I want to give a color to background and border and apply border style.

How do I do it?
Please gime me a solution.

Thanks

推荐答案

有datagrid的ItemDatabound 事件,您可以在其中访问行项目.

定义方法,然后在该方法中,仅给所需的列单元格上色.
There is ItemDatabound event of datagrid where you get accessibility to row items.

Define the method and in that method, just color the desired column cell.


使用TemplateField Inside Grid.


Use TemplateField Inside Grid.

i.e
<asp:GridView ID="grid" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate style="background-color: Green">
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            ...
            ...
            ...
        </asp:GridView>



如果有帮助,请 投票 接受答案 .



Please vote and Accept Answer if it Helped.


此外Sandeep的答案,一个快速的代码示例

这将允许您将样式应用于满足条件的某些行....因此,仅某些行将具有样式集

In addition to Sandeep''s answer, a quick code example

This would allow you to apply styles to certain rows that meet criteria....so only certain rows will have a style set

protected void GridName_RowDataBound(object sender, GridViewRowEventArgs e)
{
	if (e.Row.RowType == DataControlRowType.DataRow)
	{
		// get a reference to the data used to databound the row
		DataRowView row = (DataRowView)e.Row.DataItem;

                // Check the row, do something based on the condition of  a row value? 
                // if (row[7].ToString() == "Test")
                // {
                //     e.Row.Style.Add("Something", "class-name");
                // }
	}
}


这篇关于如何将样式仅应用于datagrid中的一个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆