Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Datagrid view in windows form and the contents of datagridview are fetched from database using query like this.
C#
//The primary Key is changed to SN using as function query here STARTS//////////

SqlCommand cmd = new SqlCommand("select CategoryId as SN, Name as CategoryName from Category", connect);
SqlDataAdapter abc = new SqlDataAdapter(cmd);

//The primary Key is changed to SN using as function query here ENDS//////////



The above code snippet produces this:

SN CategoryName
1 aaaaa
2 bbbbb
3 cccccc

Where SN is actually Primary key! I wanted SN in my gridview and modified primarykey as SN using 'AS' in query but I dont want primary key be used neither want any type of entry like SN in database.

I just want SN to be displayed in gridview.I dont know what is the actual procedure to add column in grid view .

Any help is greatly appreciated.
Posted
Updated 24-Jun-11 1:22am
v5
Comments
Slacker007 24-Jun-11 6:10am    
Edited for readability and added code block.

Here are two ways to get a column that doesn't really exist in the database. You can just pull a constant in your sql...like this:

SELECT '' as MyNewStringColumn, 0 as MyNewNumericColumn, CategoryId as SN, Name as CategoryName FROM Category

Then after you fill your DataTable, loop through it and fill the columns with what you want. But remember that you can fill that with anything accessible in SQL...so if your new column is the same thing as the SN you can just pull it twice in the SQL and give it a different alias:
SELECT CategoryId as SN, Name as CategoryName, CategoryId as SN2 FROM Category

Or if you can apply some kind of formula to it if you have one:
SELECT CategoryId as SN, Name as CategoryName, CategoryId + 1000 as SN2 FROM Category

Another method to add a column that doesn't exist in the database is this...First fill the DataTable with the sql you have. Then simply add another column to the DataTable. After that, loop through your table and for each row fill the new column with whatever you want, like this:
(It's in vb, but you should be able to get the idea)
VB
'dt is the DataTable object
dt.Columns.Add("MyNewColumn")
Dim intCount As Integer = 1
For Each row As DataRow In dt.Rows
    row("MyNewColumn") = intCount
    intCount += 1
Next


If the problem your really having is that you want the columns to have different headings...I don't think you can have DataTable columns with the same name, but you CAN set a different heading text to the columns in the DataGridView object. First you'd load your DataTable with two columns identical in all but name, then set it as the DataSource to your DataGridView, and then do this:
VB
'dgv is the DataGridView object
dgv.Columns("SN").HeaderText = "SN"
dgv.Columns("SN2").HeaderText = "SN"


Hope this helps.
 
Share this answer
 
Try This...

SqlCommand cmd = new SqlCommand("select CategoryId as SN, Name as CategoryName from Category", connect);
SqlDataAdapter abc = new SqlDataAdapter(cmd);
DataTable myTable=new DataTable();
abc.Fill(myTable);
Datagridview1.DataSource=dt;
 
Share this answer
 
Comments
shoebass 24-Jun-11 7:09am    
I didn't meant to show the content in Datagrid view I have already implemented the thing that u are telling me to do. I think U did not understood my problem the thing is I want to add something that is not present in database ...foeg what we have done till far is we just changed primary key CategoryID to SN ...but i dont want this ! I want a separate thing sn(Serial number)into gridview only not in the database! Can this be done ?
shoebass 24-Jun-11 7:09am    
and i am using datasets instead !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900