C# 根据特定单元格数据在Gridview单元格上显示不同的控件

C# 根据特定单元格数据在Gridview单元格上显示不同的控件,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个网格视图,根据数据库查询中的特定值,我希望在Score1和Score2列下显示不同类型的控件。这可以包括复选标记、标签、文本框、简单值或超链接 我的用例如下:如果score1值为空/NULL,则显示文本框,如果不是,则显示链接,否则显示一些其他控件等。。。。所以在第1列,我可能在一行有一个文本框,在另一行有一个链接 我尝试在后台代码中添加TemplateField/Itemplate,以动态添加列score1和score2。但是,我只能在Page_Load()上执行此操作,并且该列只能

我有一个网格视图,根据数据库查询中的特定值,我希望在Score1和Score2列下显示不同类型的控件。这可以包括复选标记、标签、文本框、简单值或超链接

我的用例如下:如果score1值为空/NULL,则显示文本框,如果不是,则显示链接,否则显示一些其他控件等。。。。所以在第1列,我可能在一行有一个文本框,在另一行有一个链接

我尝试在后台代码中添加TemplateField/Itemplate,以动态添加列score1和score2。但是,我只能在Page_Load()上执行此操作,并且该列只能包含一个控件。任何关于我应该如何处理这个问题的指针?

您可以使用绑定

Text="{Binding ScoreToPrint, Mode=OneWay}"
然后,您必须拥有一个可以绑定分数的属性

public String ScoreToPrint
{
    get { return _scoreToPrint }
}
或者,您可以通过抽象视图模型库中的方法和调用获得它

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;
        }
有了它,您还需要这里的中继命令类
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

编辑1:

如果您希望能够更改分数,那么您实际上需要对第一个方法进行双向绑定

Text="{Binding ScoreToPrint, Mode=TwoWay}"
您可以使用绑定

Text="{Binding ScoreToPrint, Mode=OneWay}"
然后,您必须拥有一个可以绑定分数的属性

public String ScoreToPrint
{
    get { return _scoreToPrint }
}
或者,您可以通过抽象视图模型库中的方法和调用获得它

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;
        }
有了它,您还需要这里的中继命令类
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

编辑1:

如果您希望能够更改分数,那么您实际上需要对第一个方法进行双向绑定

Text="{Binding ScoreToPrint, Mode=TwoWay}"
您可以使用绑定

Text="{Binding ScoreToPrint, Mode=OneWay}"
然后,您必须拥有一个可以绑定分数的属性

public String ScoreToPrint
{
    get { return _scoreToPrint }
}
或者,您可以通过抽象视图模型库中的方法和调用获得它

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;
        }
有了它,您还需要这里的中继命令类
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

编辑1:

如果您希望能够更改分数,那么您实际上需要对第一个方法进行双向绑定

Text="{Binding ScoreToPrint, Mode=TwoWay}"
您可以使用绑定

Text="{Binding ScoreToPrint, Mode=OneWay}"
然后,您必须拥有一个可以绑定分数的属性

public String ScoreToPrint
{
    get { return _scoreToPrint }
}
或者,您可以通过抽象视图模型库中的方法和调用获得它

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;
        }
有了它,您还需要这里的中继命令类
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

编辑1:

如果您希望能够更改分数,那么您实际上需要对第一个方法进行双向绑定

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

您可以使用gridview的RowDataBound事件并动态添加控件。唯一的缺点是大量if/switch语句&指定单元格索引

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
            }
        }
    }

您可以使用gridview的RowDataBound事件并动态添加控件。唯一的缺点是大量if/switch语句&指定单元格索引

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
            }
        }
    }

您可以使用gridview的RowDataBound事件并动态添加控件。唯一的缺点是大量if/switch语句&指定单元格索引

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
            }
        }
    }

您可以使用gridview的RowDataBound事件并动态添加控件。唯一的缺点是大量if/switch语句&指定单元格索引

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
            }
        }
    }

有很多不同的方法来解决这个问题。一种方法是将所有可能的控件放在gridview定义(aspx页面)的列中,然后只显示和使用您需要的控件。第二种方法是定义不同的列,每个列中都有一个控件,然后只使用/显示所需的列。第三种方法是在现有列中“动态”创建控件。可能还有其他一些我现在甚至没有考虑过。你的第一个建议是最简单的,我想我把所有的控件和它们的ID放在一起,然后查找它们,除了一个之外,把所有的控件都隐藏起来。然而,我真的很想学习“动态创建控制”,它似乎是最复杂的,但同时它也吸引了我。。。问题是如何编码:)正如我在过去两个小时里试图做的那样……我想根据条件(在10个不同控件之间交替)在多行gridview的一列中动态添加一个用户控件,但我失败得很惨。为了更好地理解动态控件,我了解到,除非你也有很好的理由,否则最好不要使用动态控件。读得好:真正理解动态控制-2800_第1部分\u 2900有很多不同的方法来解决这个问题。一种方法是将所有可能的控件放在gridview定义(aspx页面)的列中,然后只显示和使用您需要的控件。第二种方法是定义不同的列,每个列中都有一个控件,然后只使用/显示所需的列。第三种方法是在现有列中“动态”创建控件。可能还有其他一些我现在甚至没有考虑过。你的第一个建议是最简单的,我想我把所有的控件和它们的ID放在一起,然后查找它们,除了一个之外,把所有的控件都隐藏起来。然而,我真的很想学习“动态创建控制”,它似乎是最复杂的,但同时它也吸引了我。。。问题是如何编码:)正如我在过去两个小时里试图做的那样……我想根据条件(在10个不同控件之间交替)在多行gridview的一列中动态添加一个用户控件,但我失败得很惨。为了更好地理解动态控件,我学到了不使用动态控件通常更好吗