Link to home
Start Free TrialLog in
Avatar of udir
udir

asked on

DataGrid in RunTime...

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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Avatar of udir
udir

ASKER

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
So, you didn't see anything about setting the Sort expression, and AllowSorting = True?

Bob
Avatar of udir

ASKER

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)
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
Avatar of udir

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of udir

ASKER

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???
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

Avatar of udir

ASKER

Ohhhhhh i'm sorry it works.
Thanks a lot