It is hard to change the order in GridView. You can change the order of DataColumn in DataTable(the datasource) and set AutoGenerateColumn="true" in GridView.
Or you can create column dynamically in GridView according to your different conditions. Here is the code for create column dynamically:
public partial class DynamicallyCreateGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Columns.Clear();
CreateColumn();
}
public void CreateColumn()
{
TemplateField customField1 = new TemplateField();
customField1.ShowHeader = true;
customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Link", "", "");
customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "", "Name", "HyperLink");
GridView1.Columns.Add(customField1);
GridView1.DataSource = GetStudent("select * from table");
GridView1.DataBind();
}
public DataSet GetStudent(string query)
{
SqlConnection conn = new SqlConnection(@"Server=(local)\SQLEXPRESS;Integrated Security=SSPI;Database=test;Persist Security Info=True");
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string columnNameBinding;
private string controlType;
ashok.k
Member
94 Points
150 Posts
Changing column display order in GridView control
Apr 02, 2009 06:45 AM|LINK
Hi
I am using GridView control to display data. I want to change the order of the columns of the GridView based on the Scenario.
I am binding a Datatable which has 3 columns. (Col1 , Col2 , Col3)
I want to change the display order of the columns based on the scenario.
Scenario 1 . Col1 ,Col2 ,Col3
Scenario 2 : Col2,Col3 ,Col1
Scenario 3 : Col3,Col2,Col1
How to change the column position of a GridView control?
Thanks
Ashok
hariram.p@li...
Contributor
3366 Points
707 Posts
Re: Changing column display order in GridView control
Apr 02, 2009 07:29 AM|LINK
You can do by adding columns dynamically like this
BoundField bd = new BoundField();bd.DataField =
"ProductID"; bd.HeaderText = "ProductID";grdView.Columns.Add(bd);
Hari
---------------------------------------------
Dont forget to click "Mark as Answer" on the post that helped you.
My Site
santhosh11s
Member
705 Points
158 Posts
Re: Changing column display order in GridView control
Apr 02, 2009 07:36 AM|LINK
Hai,
Check this link
hope this helpshttp://community.devexpress.com/forums/t/70628.aspx
hariram.p@li...
Contributor
3366 Points
707 Posts
Re: Changing column display order in GridView control
Apr 02, 2009 07:49 AM|LINK
Hi Santhosh
There is no such property VisibleIndex in GridView
Hari
---------------------------------------------
Dont forget to click "Mark as Answer" on the post that helped you.
My Site
ashok.k
Member
94 Points
150 Posts
Re: Changing column display order in GridView control
Apr 02, 2009 03:12 PM|LINK
Hi all,
Thank you for your replies.. Visible index property is not there in GridView in .NET 3.5.
I am adding bound columns and template columns to GridView at design time.
Is it possible to Remove the columns added at design time?
And also is it possible use GridView.Columns["name"].Add/Insert and GridView.Columns["name"].Remove to change the column position?
Can i remove the Column called Id at position 4 and insert in again at position 1.?
Thanks
Ashok
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Changing column display order in GridView control
Apr 07, 2009 07:47 AM|LINK
Hi ashok.k,
It is hard to change the order in GridView. You can change the order of DataColumn in DataTable(the datasource) and set AutoGenerateColumn="true" in GridView.
Or you can create column dynamically in GridView according to your different conditions. Here is the code for create column dynamically:
public partial class DynamicallyCreateGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Columns.Clear();
CreateColumn();
}
public void CreateColumn()
{
TemplateField customField1 = new TemplateField();
customField1.ShowHeader = true;
customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Link", "", "");
customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "", "Name", "HyperLink");
GridView1.Columns.Add(customField1);
GridView1.DataSource = GetStudent("select * from table");
GridView1.DataBind();
}
public DataSet GetStudent(string query)
{
SqlConnection conn = new SqlConnection(@"Server=(local)\SQLEXPRESS;Integrated Security=SSPI;Database=test;Persist Security Info=True");
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string columnNameBinding;
private string controlType;
public GridViewTemplate(DataControlRowType type, string colname, string colNameBinding, string ctlType)
{
templateType = type;
columnName = colname;
columnNameBinding = colNameBinding;
controlType = ctlType;
}
public void InstantiateIn( System.Web.UI.Control container )
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
if (controlType == "Label")
{
Label lb = new Label();
lb.ID = "lb1";
lb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(lb);
}
else if (controlType == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "tb1";
tb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(tb);
}
else if (controlType == "CheckBox")
{
CheckBox cb = new CheckBox();
cb.ID = "cb1";
cb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(cb);
}
else if (controlType == "HyperLink")
{
HyperLink hl = new HyperLink();
hl.ID = "hl1";
hl.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(hl);
}
break;
default:
break;
}
}
public void ctl_OnDataBinding(object sender, EventArgs e)
{
if (sender.GetType().Name == "Label")
{
Label lb = (Label)sender;
GridViewRow container = (GridViewRow)lb.NamingContainer;
lb.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
else if (sender.GetType().Name == "TextBox")
{
TextBox tb = (TextBox)sender;
GridViewRow container = (GridViewRow)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
else if (sender.GetType().Name == "CheckBox")
{
CheckBox cb = (CheckBox)sender;
GridViewRow container = (GridViewRow)cb.NamingContainer;
cb.Checked = Convert.ToBoolean(((DataRowView)container.DataItem)[columnNameBinding].ToString());
}
else if (sender.GetType().Name == "HyperLink")
{
HyperLink hl = (HyperLink)sender;
GridViewRow container = (GridViewRow)hl.NamingContainer;
hl.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
hl.NavigateUrl = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
}
}
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework