简体   繁体   English

如何更改DataBound GridView上的标签?

[英]How can I change the label on a DataBound GridView?

I inherited an application from another developer written in vb.net(which I'm still not too fluent in). 我继承了另一个用vb.net编写的开发人员的应用程序(我仍然不太熟练)。 It contains a GridView which is databound and it generates the label of the columns dynamically as well. 它包含一个被数据绑定的GridView,它也动态地生成列的标签。 I need to change the Academic Year headings to read, for example, as 2009-2010 instead of simply 2010. The current code sets the academic year for the heading, but it also seems to control the data being displayed as well. 我需要将“学术年”标题更改为例如2009-2010,而不是简单的2010。当前代码设置了该学科的学年,但它似乎也可以控制所显示的数据。

Here is the code: 这是代码:

sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year & "], "
sql_string_Total = sql_string_Total & " sum([" & Academic_year & "]-[" & Academic_year - 1 & "]) as [" & (Academic_year - 1) & "-" & (Academic_year) & " Diff.], "
sql_string_Total = sql_string_Total & " (cast(((sum([" & Academic_year & "])-sum([" & Academic_year - 1 & "]))/(sum([" & Academic_year - 1 & "])*1.0)*100.0)as numeric(4,1))) as [" & (Academic_year - 1) & "-" & (Academic_year) & " % Change] "

How can I change the headings of these cols without affecting the data displayed? 如何在不影响所显示数据的情况下更改这些列的标题?

This seems to be more a mixture of SQL-VB question rather than just a VB one. 这似乎更多是SQL-VB问题的混合,而不仅仅是VB问题。 The as keyword in SQL indicates the name the field will have in the resultset. SQL中的as关键字指示该字段将在结果集中具有的名称。 Having said this... 说了这么多...

quick solution: just change the name of the field as you need it. 快速解决方案:只需根据需要更改字段名称。 For example, change: 例如,更改:

sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year & "], "

to

sql_string_Total = sql_string_Total & " sum([" & Academic_year - 4 & "]) as [" & Academic_year - 5 & "-" & Academic_year - 4 & "],sum([" & Academic_year - 3 & "]) as [" & Academic_year - 4 & "-" & Academic_year - 3 & "],sum([" & Academic_year - 2 & "]) as [" & Academic_year - 3 & "-" & Academic_year - 2 & "],sum([" & Academic_year - 1 & "]) as [" & Academic_year - 2 & "-" & Academic_year - 1 & "],sum([" & Academic_year & "]) as [" & Academic_year - 1 & "-" & Academic_year & "], "

The previous developer uses the same variable to both the column to sum and the name the sum will have, but you can change the name to whatever you need without affecting the data that will be queried. 以前的开发人员对要求和的列和将要具有的名称都使用相同的变量,但是您可以将名称更改为所需的名称,而不会影响要查询的数据。 In the gridview, the column will take the name you specified for the resulting column. 在gridview中,该列将采用您为结果列指定的名称。

slow, recommended solution: rewrite that piece of code. 缓慢的推荐解决方案:重写那段代码。 I know it's not yours, but as you can see, it's a little monster to read and any edit makes it even worse; 我知道这不是你的,但正如你所看到的,它是一个小怪物,任何编辑都会使它变得更糟。 not to mention that it's a very very bad approach for creating a dinamyc sql. 更不用说这是创建dinamyc sql的非常非常糟糕的方法。 Looking for a way to rewrite it can also help you improve your VB.net skills, and I have a feeling you will need them if you are going to be hanging around with this project for a while. 寻找一种重写方式也可以帮助您提高VB.net技能,如果您要在这个项目中待一会儿,我认为您将需要它们。 I suggest you to take a look to the String.Format method and Parameterized Queries for a start. 我建议您先看看String.Format方法和参数化查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM