Amending Gridview according to table properties
I shall attempt to explain the scenario.
I have a gridview I wish to amend according to the preferences a user may set. These preferences being stored within a table in a EDMX (tblPref). The table gives the prefernce i.e. Product, the Alias for the preference i.e. SKU and whether the Preference should be shown i.e. ShowProduct?
So for example, the 'standard grid' would look like this:
Product UUF1 UUF2
1 a d
2 b e
3 c f
tblPreference would look like the following:
Preference Alias ShowPreference
Product Product 1
UUF1 UUF1 1
UUF2 UUF2 1
However, it may be the case that user may wish to see different columns i.e.
Product UUF2
1 d
2 e
3 f
tblPreference would look like the following:
Preference Alias ShowPreference
Product Product 1
UUF1 UUF1 0
UUF2 UUF2 1
And/Or they wish to label the headers with different text i.e.
SKU Category Sub-Category
1 a d
2 b e
3 c f
tblPreference would look like the following:
Preference Alias ShowPreference
Product SKU 1
UUF1 Category 1
UUF2 Sub-Category 1
Now then, I realise that it is possible to hide columns (gridview.columns[0].Visbile = False) and that I can amend header text (gridview.HeaderRow.Cells[0].Text = "Sku").
What I am unsure is how I bring it altogether...the important part!
Obviously a fair few 'ifs' will be involved but I was wondering if anyone had done anything like this before and could provide a bit of pseudo code?
The reason I am going down this route is that I may have several hundred users who could potentially want the gridview to look completely different to them. Being a newbie, it is als开发者_运维技巧o good for me to try more difficult things rahter than 'dragging and dropping'.
Therefore any help provided will be gratefully received.
If you have your grid set up like this in the aspx file.
<asp:GridView runat=server ID=grid1 AutoGenerateColumns=false></asp:GridView>
Then you should be able to use the code in the following to dynamically create the fields that are needed in the grid.
using (SqlConnection conn = new SqlConnection("MySqlConnectionString"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tblPreference"); // you will also have to limit this selection to only rows for this user.
cmd.Connection = conn;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
if(dr.GetInt32(dr.GetOrdinal("ShowPreference"))==1) //not sure if this is a bit or int field
{
grid1.Columns.Add(new BoundField(){
HeaderText = dr.GetString(dr.GetOrdinal("Alias")),
DataField = dr.GetString(dr.GetOrdinal("Preference"))
});
}
}
}
}
While I'm sure this will have to be adapted a bit for your specific use. It should give you a good start. Note that after running the above to set up your columns, you will still need to provide the grid with data either with a DataSource control or by manually binding it like below
grid1.DataSource = MyDataReader; //or data table, or collection of objects
grid1.DataBind();
You might try defining your columns programatically in the code behind, rather than declaratively in the aspx file. You could add an handler for the the DataBound event perhaps, and in it you could create your BoundFields with whatever header text you need.
精彩评论