0
votes

I have a grid-view in which based on certain value from my database query, I want to display different kind of control under Score1 and Score2 columns. This can range anything between check mark, label, text box, simple value or hyperlink.

My use case is as follow: if score1 value is empty/NULL, display text box, if it is not then display a link, otherwise display some other controls ect.... so on the column score 1 , i may have a text box on one row, a link on another row.

enter image description here

I've tried to add TemplateField/Itemplate in the behind code to dynamically add the columns score1 and score2. However, I can only do this at Page_Load() and the column can only contain one control. Any pointer on how I should approach this?

2
There are a lot of different approach to this problem. One could be to put all possible controls in the column in the gridview definition (aspx page), then show and use only the ones you need. A second one could be to define different columns each with one control in it, then use/show only the column you need. A third one could be to create controls "on the fly" in an existing column. There are probably other ones I even don't think about right now. - Laurent S.
Your first suggestion is the simplest, in which I think i put all the control in with their ID and look them up and hide all but one. However, I really want to learn the "create control on the fly", it seems to be the most complex but at the same time it intrigues me... the problem is how to code it up :) as that what I was trying to do for the last 2 hours... - Fylix
I wanted to add one user controls dynamically in one column of a gridview with multiple rows, depending on a condition (alternating between 10 different controls) but I was failing miserably. In an effort to understand dynamic controls better I learned that is it often better to not use dynamic controls unless you have a very good reason too. Good read: TRULY Understanding Dynamic Controls - weblogs.asp.net/infinitiesloop/…2800_Part-1_2900 - Doreen

2 Answers

2
votes

You can use binding.

Text="{Binding ScoreToPrint, Mode=OneWay}"

Then you have to have a property that the score can bind to.

public String ScoreToPrint
{
    get { return _scoreToPrint }
}

Alternatively you can get it with methods and calls within an abstracted View Model Base.

public ICommand PrintText
        {
            get
            {
                if (_printText == null)
                {
                    _printText = new RelayCommand(p => PrintText(p as Control), p => CanPrintText(p as Control));
                }

                return _printText;
            }
        }

        protected abstract void PrintText(Control control); //Where you instantiate what it should do in a child class

        protected virtual bool CanPrintText(Control control)
        {
            return true;
        }

With this you will also need the Relay Command Class which is here http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

EDIT 1:

You would actually want 2-way binding on the first method if you are wanting to be able to change the score.

Text="{Binding ScoreToPrint, Mode=TwoWay}"
1
votes

You can use RowDataBound event of the gridview & add controls dynamically. Only down side is lot of if/switch statements & specifying cell index

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // You can replace this with a switch statement
            if (DataBinder.Eval(e.Row.DataItem, "Discontinued").ToString() == "False")
            {
                TextBox txtTemp = new TextBox();
                txtTemp.Text = "I am a textbox";
                e.Row.Cells[10].Controls.Add(txtTemp);
            }
            else
            {
                // Add other controls here
            }
        }
    }