开发者

How do I set variables to gridview cells?

开发者 https://www.devze.com 2023-02-17 12:42 出处:网络
I\'m relatively new to ASP, so this may be a simple fix that I am just not figuring out (here\'s hoping!).I am working in VisualStudio2010 using C#.

I'm relatively new to ASP, so this may be a simple fix that I am just not figuring out (here's hoping!). I am working in VisualStudio2010 using C#.

Basically, I have a table in SQL with the following columns: Product, ProductGroup, Attribute1Value, Attribute2Value, Attribute3Value

For each ProductGroup, the Attribute definition is different. For instance, in ProductGroup1, Attribute1 is Size and Attribute1Value might be 2,3,5, etc. In ProductGroup2, Attribute1 is Quality and the Attribute1Values would be Standard, Economy, Premium, etc.

I will only ever pull in one Product Group at a time and would like to have my column headers reflect the Attribute definition (Header= Size rather than Attribute1Value when applicable".

What I would like to do is pull in a gridview that is invisible with the columns ProductGroup, Attribute1, Attribute2, Attribute3 and then assign the values within the column to be variables that I can use to reassign my column headers. I have been able to set column headers based on other variables (specifically the selected value from a dropdown), but am having trouble with the code to set variables equal to gridview values.

When I tried

    protected void Page_Load(object sender, EventArgs e)
    {
        lblAttribute1.Text = GridView4.Rows[0].Cells[2].Text;
    }

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
  开发者_C百科          e.Row.Cells[3].Text = lblAttribute1.Text;
        }
    }

I got the error message that my index was out of range. I thought at first that this may be because my Gridviews have no rows upon page load, only after a few selections from DropDowns, but I don't know if they are at all related. Is there a better way to do this in general?

Sorry if that was poorly phrased. Please let me know what other information you need.

Thanks so much for your help!


Perhaps you can try creating classes specific to your different Product Groups, and then bind these to your grid as appropriate (as opposed to binding the raw data from sql).

For example, you said ProductGroup1's attribute represents Size. So create a class which is a clearer representation of ProductGroup1, like:

public class ProductGroup1
{
  int Id{get;set;}
  int Size{get;set;}
}

Do the same for your other ProductGroups (i.e. ProductGroup2 class would have a Quality property).

You mentioned you were only going to be binding one product group at a time, so I am assuming you would have db queries that are selecting data by ProductGroup. In this case, take the returned records from your sql query and convert/map each record to one of your classes (eg. ProductGroup1 above).

You should then end up with a List or Array of ProductGroup classes. You can then Bind this List or Array to your Data Grid as is, using auto binding on the column headers, as they will match whatever properties you have defined in your class.


Have you tried the following:

  1. Pull in the ProductGroup info separately from the Product records (since there's only one for all of the Product records.
  2. Use the ProductGroup record to set the HeaderText values for your Gridview columns.
  3. Bind the grid to the Products.

You can set the column's HeaderText like this:

Gridview4.Columns[2].HeaderText = attribute1ValueFromProductGroupRecord

You only need to do this once for each column, maybe in Page_Load.

0

精彩评论

暂无评论...
验证码 换一张
取 消