在数组的基础上显示gridview数据

皮肤

我有一个想要显示的按钮 gridview

以前它是根据 Datatable

但是现在我想在 Array

这是代码

protected void btnSubmit_Click(object sender, EventArgs e)
{
    var selectedItems = cmbEmp_Name.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Value).ToArray();
    var result = String.Join(",", selectedItems);

    string[] StrAreaInt = result.Split(new char[] { ',' });

    for (int i = 0; i < StrAreaInt.Length; i++)
    {
        Muster_Process(StrAreaInt[i]); // length comes here
        HiddenDiv.Visible = true;
        grdMonthlyProc.Visible = true;
    }
}
Fnostro

.aspx标记。

这些是我认为您正在使用的控件。

    <asp:CheckBoxList ID="cmbEmp_Name" runat="server" RepeatLayout="Flow"></asp:CheckBoxList>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    <asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="true"></asp:GridView>

背后的代码。

确保仅分配一次数据源,否则您将在每次回发中丢失所选的项目。

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    // Changes these to your field names
    cmbEmp_Name.DataTextField= "Name";
    cmbEmp_Name.DataValueField = "DoctorID";

    // This code populate the CheckBoxList in my own project
    // Your Post doesn't show how you populate your control
    // I assume it's being done elsewhere in your code.
    // But it is important to understand that if you do not load
    // the CheckBoxList within a `if (!Page.IsPostBack)` block anything
    // you select in the list will be lost when you click the button
    // because the control will be repopulated on Postback.

    cmbEmp_Name.DataSource = ef6Context.GetListOfNames().ToList();
    cmbEmp_Name.DataBind();      
  }
}

//Button1 Postback bind the Gridview.
protected void Button1_Click(object sender, EventArgs e)
{
  // Once you select a number of items from `cmbEmp_Name` and hit your
  // button this statement will create and Array of `ListItem`s
  grdMonthlyProc.DataSource = cmbEmp_Name.Items
                                  .Cast<ListItem>()
                                  .Where(li => li.Selected)
                                  .ToArray();

  // And this will Bind the Array to the GridView.  Internally GridView 
  // will extract all Public Properties of a ListItem and automatically
  // generate a default set of Columns to display.  With a simple
  // redefinition you can display specific columns, but more on that later
  grdMonthlyProc.DataBind();

}

您的帖子中的代码块做了太多的工作:

  1. 您正在拉出所有Selected ListItems
  2. 然后提取Value属性
  3. 然后创建一个数组 Value
  4. 然后使用join创建一个逗号分隔的字符串 Value
  5. 仅将其分割回另一个数组 Value

然后您有一个for循环。我不知道为什么,但是您打电话给Muster_Process,我认为这是相关的,但是您不说它的作用。然后,您将“可见性”重复设置为循环内的相同控件(HiddenDivgrdMonthlyProc),这毫无用处。在循环外一次就足够了。

但是,在帖子的第一行中,您提到要填充GridView,但是没有说要显示的内容。

定义如下grdMonthlyProc将显示ListItems的所有公共属性:

    <asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="true">
    </asp:GridView>

重新定义为此(请注意此更改AutoGenerateColumns="false":),它将仅显示Value属性:

    <asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="false">
      <Columns>
        <asp:BoundField DataField="Value"  HeaderText="Value" />
      </Columns>
    </asp:GridView>

并且添加其他内容BoundField将仅显示“值”字段和“文本”字段:

    <asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="false">
      <Columns>
        <asp:BoundField DataField="Value"  HeaderText="Value" />
        <asp:BoundField DataField="Text"  HeaderText="Text" />
      </Columns>
    </asp:GridView>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在列的基础上的数据帧添加或Subract两列?

在增加索引号的基础上连接熊猫数据框并在此基础上保留它们的位置

如何确定数字和字母组合在一起的数组的组成部分是否在数字的基础上彼此接替?

在基础上安装桌面

在前两个数组的基础上创建第三个数组

在交互式基础上编写提交消息时显示差异

如何使用angularjs在条件基础上显示特定的JSON对象键

如何在单选按钮选择的基础上显示和隐藏div

Yii2显示相关数据不在数组中,未使用Listview或Gridview

如何在对象ID的基础上从对象数组中过滤出对象

我在php中有多维数组,想在3列的基础上删除重复的条目

如何在一个空数组的基础上更改UItableview中的NumberOfRowsInSection

在键的基础上转换JSON对象的JSON对象ot数组-Python中的代码

在每个目录的基础上配置ack

本机基础上的多个输入

在map()的基础上使用JS reduce()

基础上的全高柱

如何在使用* args的函数中基于年份在基于数据帧的基础上删除行

我想在熊猫中的另一列数据的基础上添加新列

我的来源是在什么基础上对数据进行矢量化和聚类?

在两列的基础上合并两个数据框,同时考虑 nan 值

在条件基础上更改列的值(缺少数据和分类)

如何在React.js的菜单组件基础上更改Home组件中的数据

Perl - 在每个 x 维度的基础上转置 CSV 数据

SparkSQL在现有列的基础上向数据框添加新列

数据帧计数一组条件在每行的基础上由几列传递

在数据网格上显示数据太慢

如何在第一个数组的基础上合并两个数组

比较来自在Java不同数据的基础上的两个数据集(两个结果集)