?
Solved

DataGrid in RunTime...

Posted on 2007-07-28
10
Medium Priority
?
483 Views
Last Modified: 2008-01-09
Hi,
I create a DataGrid at RunTime.
I bound data like :
............
            dataGrid.DataSource = ds.Tables[0];
            dataGrid.DataBind();

but now i also want to let the user:
1) sort rows (by clicking the Header)
2) the first Columns is "ID", and i want it to be like a "Link Lable".
(I did this when i create a dataGrid in the aspx file, but bound each columns, but i don't know how to do it at RunTime).
Thanks
0
Comment
Question by:udir
  • 5
  • 4
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19587375
0
 
LVL 1

Author Comment

by:udir
ID: 19588515
Hi,
What I didn't get from all those articles is how to turn a column to a sortable column (which i understand turn automatically the Header into LinkLable).
Can u give an example of that?
Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19588640
So, you didn't see anything about setting the Sort expression, and AllowSorting = True?

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:udir
ID: 19590241
Maybe i didn't understand it from there...
anyway i wrote :
        //The DataBind :    
           view = new DataView(ds.Tables[0]);
            dataGrid.DataSource = view;
            dataGrid.DataBind();
       //Make the DataGrid Sortable:
            dataGrid.AllowSorting = true;
      //Bound a column in order to make it Sortable:
            BoundColumn id = new BoundColumn();
            id.HeaderText = "Þáäè èÙéÕÝ";
            id.DataField = "UserID";
            id.SortExpression = "UserID";
            dataGrid.Columns.AddAt(0, id);

(In my Table there are 3 Columns : UserID, UserName, Password)
What did i missed here? (becuase the Header of the column didn't turn into LinkLable, which means the Columns is sortable)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19591233
If you are adding columns after you call DataBind, it isn't going to work.  The DataGrid needs to be completely set up before calling DataBind.

Bob
0
 
LVL 1

Author Comment

by:udir
ID: 19591582
Hi,
so in that case, can i make columns sortable after binding a table to dataGrid (i mean - the hole table, like i did), and not bind each column?
if i can, pls, can u give an example?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19591778
You can change the SortExpression for a column, but you would need to rebind the DataGrid.  You need to set all properties for the DataGrid before you call DataBind--period.

Bob
0
 
LVL 1

Author Comment

by:udir
ID: 19591882
Well, i changed it like this :
     //properties :
       ...........
     //Bind the firt column and make it sortable :
       BoundColumn id = new BoundColumn();
            id.HeaderText = "UserID";
            id.DataField = "UserID";
            id.SortExpression = "UserID";
            dataGrid.Columns.AddAt(0, id);
      //call DataBind :
       string selectCommand = "SELECT  * from Users"
            Database db = DatabaseFactory.CreateDatabase("connection");
            ds = db.ExecuteDataSet(CommandType.Text, selectCommand);

            dataGrid.DataSource = ds.Tables[0];
            view = new DataView(ds.Tables[0]);
            dataGrid.DataSource = view;
            dataGrid.DataBind();
------------------------------------------

But what i'v got is at the first column - "UserID" and then again UserID, UserName, Password.(but the header is not in a LinkLable, which mean as i understand - the column didn't turn to be sortable)
I don't want to add another column, i just want to turn column - UserID into sortable (and make the header be a link lable.
What i did wrong???
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19591961
1) Can you show me how the Data is declared in the HTML designer?

2) You are setting DataSource more than once:

    dataGrid.DataSource = ds.Tables[0];
    dataGrid.DataSource = view;

3) This doesn't look like it needs to be a dynamic column setting, since you can do this easily in the grid designer.

Bob

0
 
LVL 1

Author Comment

by:udir
ID: 19592121
Ohhhhhh i'm sorry it works.
Thanks a lot
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question