Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamically define GridView columns
Message
From
08/07/2006 11:02:11
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
07/07/2006 16:24:55
Jerry Tovar
Dana Corporation Dana It
Maumee, Ohio, United States
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
Miscellaneous
Thread ID:
01134578
Message ID:
01134680
Views:
17
This message has been marked as the solution to the initial question of the thread.
>Using ASP.Net 2.0, can I dynamically define a GridView's columns on a WebForm at runtime?
>
>For example, I have an order status WebForm that shows order history, back orders, credits etc... If a user selects the back order view button, I would like to show different columns of data in the GridView than if he selected the credit view button. My GridView control exists on the WebForm, I would just like to display different columns at runtime.
>
>It would be similar to a VFP WinForm and defining a grids columns at runtime as such:
>
>
>       ***VFP Code
>       mygrid.COLUMN1.CONTROLSOURCE  = "orderno"
>       mygrid.COLUMN2.CONTROLSOURCE  = "partno"
>       mygrid.COLUMN3.CONTROLSOURCE  = "desc"
>
>
>Can the same thing be done in a ASP.Net WebForm. Or what other ways can I accomplish show different columns in a GridView at runtime.
>
>Thanks,
>
>Jerry

Jerry,
You can put a single GridView and dynamically change its content as in VFP. ie:
private SqlDataSource sds;

private void QueryData(string sql)
{
 ConnectionStringSettingsCollection csc = ConfigurationManager.ConnectionStrings;
 ConnectionStringSettings cs = csc["NorthwindConnectionString"];
 sds = new SqlDataSource(cs.ConnectionString, sql);
}

protected void btnShowCompany_Click(object sender, EventArgs e)
{
  QueryData("select customerID,CompanyName from customers");
  myGridView.DataSource = sds;
  myGridView.AutoGenerateColumns = false;
  myGridView.Columns.Clear();
  AddField(myGridView,"customerID","customer\'s ID");
  AddField(myGridView, "companyName", "Company Name");
  myGridView.DataBind();
}
protected void btnShowContact_Click(object sender, EventArgs e)
{
  QueryData("select customerID,ContactName,ContactTitle from customers");
  myGridView.DataSource = sds;
  myGridView.AutoGenerateColumns = false;
  myGridView.Columns.Clear();
  AddField(myGridView,"customerID","customer\'s ID");
  AddField(myGridView, "contactName", "Contact Name");
  AddField(myGridView, "contactTitle", "Contact Title");
  myGridView.DataBind();
}

private void AddField(GridView gv, string dataField, string caption)
{
      BoundField bf = new BoundField();
      bf.DataField = dataField;
      bf.HeaderText = caption;
      gv.Columns.Add(bf);
}
PS: This is a 'quick' sample code. Probably you'd use select * version and use cached data instead.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform