在运行时动态更改gridview的列名所需 [英] Required to change the column name of gridview dynamically at runtime
本文介绍了在运行时动态更改gridview的列名所需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是,我需要更改gridview(与XMLTextReader绑定)的列名(格式为删除第一个字符并在第三个字符后插入冒号"),而不必直接更改XML文件.我需要在运行时动态更改列名.
Performance.xml
Hi,
My issue is that I need to change the column name(following format "Remove first character and after third character insert colon") of the gridview (which is bound with XMLTextReader), without directly changing the XML file. I am required to change the column name dynamically at runtime.
Performance.xml
<Performance>
<Departments>
<Heading>FS</Heading>
<S0015>1</S0015>
<S0020>2</S0020>
<S0025>5</S0025>
<S0030>5</S0030>
<S0035>6</S0035>
</Departments>
<Departments>
<Heading>BS</Heading>
<S0015>0</S0015>
<S0020>3</S0020>
<S0025>5</S0025>
<S0030>1</S0030>
<S0035>3</S0035>
</Departments>
</Performance>
Heading S0015 S0020 S0025 S0030 S0035
FS 1 2 4 5 6
BS 0 3 5 1 3
Required Format:"Remove first character and after third character insert colon"(S0015-- 00:15)
Heading 00:15 00:20 00:25 00:30 00:35
FS 1 2 4 5 6
BS 0 3 5 1 3
Kindly look on this issue.
推荐答案
您好,我知道如何更改列名,有人可以帮我解决一下更改格式(截断第一个字符并在第三个字符后插入冒号) ).
字符串操作?
Hi, I got how to change the column name, Can somebody help me out regarding changing the format ( Truncating first character and inserting colon after third character).
String Manipulation?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
string x = e.Row.Cells.Count.ToString();
int y = System.Int32.Parse(x);
for (int i = 2; i < y; i++)
{
e.Row.Cells[i].Text = "xxx";
}
}
}
使用Insert()函数将冒号放入:
Use the Insert() function to put the colon in:
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Insert(3,":");
使用SubString()函数进行截断:
Use the SubString() function to do the truncation:
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Substring(1);
您可以将这两个功能合并为一个语句:
You can combine these two functions into a single statement:
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Insert(2,":").Substring(1);
一种实现方法如下:
One way to do that is as follows:
var bookquery = from k in XElement.Load(MapPath("book.xml")).Elements("book")
join m in XElement.Load(MapPath("Library.xml")).Elements("Library")
on (String)k.Element("bookid") equals (String)m.Element("bookid")
select new bookclass
{
bookid = (String)m.Element"bookid"),
Title = (String)k.Element("title"),
AuthorName = (String)k.Element("author").Element("first-name"),
AuthorSurName = (String)k.Element("author").Element("last-name"),
Price = (String)k.Element("price")
};
GridView1.DataSource = bookquery;
GridView1.DataBind();
这篇关于在运行时动态更改gridview的列名所需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文