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
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
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
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
Bob
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)
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
Bob
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.CreateData base("conn ection");
ds = db.ExecuteDataSet(CommandT ype.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???
//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.CreateData
ds = db.ExecuteDataSet(CommandT
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
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
ASKER
Ohhhhhh i'm sorry it works.
Thanks a lot
Thanks a lot
Did you look at the properties of the datagrid (allowsorting = true ?)
Dynamic Column Sorting and Paging in ASP.NET
http://www.developerfusion.co.uk/show/4686/
http://msdn2.microsoft.com/EN-US/library/aa984282(VS.71).aspx
For your second question, you'll need a template column.
http://www.cambiaresearch.com/c4/d85cf70a-072f-48f7-8d79-939d6ed1e514/Custom-HyperLink-Column-for-DataGrid.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.templatecolumn.itemtemplate.aspx