Answered by:
Gridview - Adding new column

Question
-
Hello,My question is: Can I add in new column(S) in the gridview if the column is not found in the database?- E.g. An indicator if a certain lecturer is a ML/Lec (roles).I have a gridview that is "extracted" out from the database.But I want to add in a new row which is not in the database but links to the data in the database.For example, in the database, I know that a certain lecturer is a module leader, staff.But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.Maybe the logic is if a certain column is diplayed, then show a column (create own new one) and display tick, mark or ML/LEC/null.But i am not sure how to do this.Any help appreciated.Need me to further explain my queries, let me know. :)Wednesday, September 9, 2009 1:45 AM
Answers
-
Hi,
You should be able to add a new column to the grid using grid.Columns.Add(), however this might not be the ideal solution to your problem since you're binding the grid and probably should bind a value in the underlying data source for the new column too.
Three other options are;
1. Add a 'view' to the database (if you're allowed) which has the additional column... this would be a calculated column that would exist on in the view and not in the table, this might be allowed.
2. Add a calculated column to the query that returns your data, i.e if your select is
select id, name, class from lecturers
And you have a 'roles' table that defines the lecturer as a 'leader' you could add a calculated column like this;
select id, name, class, case when isnull(r.Id, 0) = 0 then 0 else 1 end as IsLeader
from lecturers l
left join roles r on l.RoleId = r.Id and r.Name = 'Leader'
This doesn't add a column to the database, but does return a new column from your query that is calculated when the query is run. That column (IsLeader) then turns up in your dataset, and when you bind the grid to the data set the column should appear like all the others.
I'd need to know more about how you 'know that a certain lecturer is a module leader, staff' before I could give you an exact sql statement, if that's what you require.
3. Bring back the data that says whether or not the lecturer is a leader, add the column to the dataset after you run the query, then loop through all the rorws and set the value of the new column in the data set based on the other columns/rows, then call acceptchanges before binding to the grid. This would be the least effiicent approach though.- Proposed as answer by Cooper Cuong Tran Wednesday, September 9, 2009 3:27 AM
- Marked as answer by liurong luo Monday, September 14, 2009 3:26 AM
Wednesday, September 9, 2009 1:57 AM
All replies
-
Hi,
You should be able to add a new column to the grid using grid.Columns.Add(), however this might not be the ideal solution to your problem since you're binding the grid and probably should bind a value in the underlying data source for the new column too.
Three other options are;
1. Add a 'view' to the database (if you're allowed) which has the additional column... this would be a calculated column that would exist on in the view and not in the table, this might be allowed.
2. Add a calculated column to the query that returns your data, i.e if your select is
select id, name, class from lecturers
And you have a 'roles' table that defines the lecturer as a 'leader' you could add a calculated column like this;
select id, name, class, case when isnull(r.Id, 0) = 0 then 0 else 1 end as IsLeader
from lecturers l
left join roles r on l.RoleId = r.Id and r.Name = 'Leader'
This doesn't add a column to the database, but does return a new column from your query that is calculated when the query is run. That column (IsLeader) then turns up in your dataset, and when you bind the grid to the data set the column should appear like all the others.
I'd need to know more about how you 'know that a certain lecturer is a module leader, staff' before I could give you an exact sql statement, if that's what you require.
3. Bring back the data that says whether or not the lecturer is a leader, add the column to the dataset after you run the query, then loop through all the rorws and set the value of the new column in the data set based on the other columns/rows, then call acceptchanges before binding to the grid. This would be the least effiicent approach though.- Proposed as answer by Cooper Cuong Tran Wednesday, September 9, 2009 3:27 AM
- Marked as answer by liurong luo Monday, September 14, 2009 3:26 AM
Wednesday, September 9, 2009 1:57 AM -
Hi,
No, by View I meant a Sql Server View... you can only use those if you are using Sql Server (although other databases like Oracle might also have them), however it will mean a change to the database, just not adding a column to an existing table. Whether you can do that depends on whether adding a view is acceptable or not.
Can you post the names of the tables and their columns ? If I had those I could possibly write you a sql statement that would return the extra column as a calculated value.
Your posted code appears to be loading drop down lists, not a DataGridView... am I missing something ? Can you post the sql statement used to load the DataGridView ? That might also help.Thursday, September 10, 2009 3:38 AM -
Sorry, is it possible for me to send through somewhere else.
I am afraid that I cant post too much codes here. =X
But, when you want to post your reply, you can post it here if you wish others to learn about this too. :)Thursday, September 10, 2009 4:07 AM -
Sorry, I'm loathe to post an email address in the forums for fear of spam.
Can you modify your select statement that retrieves the data for the data grid view to look something like the following ?
SELECT a.Subject, a.Lecturer, CASE WHEN a.Lecturer = b.ModuleLeader THEN 1 ELSE 0 END AS IsModuleLeader
FROM [TableA] a
LEFT JOIN [TableB] b ON a.[Subject] = b.[Subject]
Based on the table structure you described, something like that should return you three columns; Subject, Lecturer and IsModuleLeader which will be 1 if the lecturer is a module leader and 0 if they aren't (which should be good values to put in a check box).
Can you modify your statement based on the sample I've provided ?Thursday, September 10, 2009 4:17 AM -
I have the same problem that i need to add new column to the datagridview at runtime. Is it possible?
for example : In "Emp" table, i have "Empid","Ename","Sal","Deptno " . Here i need add new column "Loc " permanently to the database. How?
---Sri'sThursday, September 10, 2009 3:09 PM -
Hello My Best Solutions,
Please follow up the thread below for your issue:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f191e3a5-a159-44f0-ae24-fb692dc818fb
Thanks a lot!
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
Welcome to the All-In-One Code Framework!Monday, September 14, 2009 6:35 AM