简体   繁体   中英

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). It contains a GridView which is databound and it generates the label of the columns dynamically as well. 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.

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. The as keyword in SQL indicates the name the field will have in the resultset. 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.

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. 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. I suggest you to take a look to the String.Format method and Parameterized Queries for a start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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