在我的情况下如何将List <object>转换为List <string []>?

时间:2017-06-29 08:49:04

标签: c# asp.net list webforms

我正在尝试从 devexpress AspxGridView 中获取所选行的字段值。

  

我设置在AspxGridView中只选择一行而不是多行。然后我想在服务器端获取字段值。用于测试我正在尝试打印button.text

这是我的Aspx代码

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" Width="100%" SettingsBehavior-AllowSelectByRowClick="True" OnSelectionChanged="btnSearch_Click">
    <Settings HorizontalScrollBarMode="Visible" ShowFilterRow="True" ShowGroupedColumns="True" ShowTitlePanel="True" ShowGroupPanel="True" />

    <SettingsBehavior AllowSelectSingleRowOnly="True" />   --it will select only one row

    <SettingsSearchPanel Visible="True" />

    <Columns>
        <dx:GridViewDataTextColumn FieldName="Status" ReadOnly="True" VisibleIndex="0">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="WorksheetID" VisibleIndex="1">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="POTitle" VisibleIndex="2" Width="200px" ExportWidth="100" MinWidth="100">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="FromStoreName" VisibleIndex="3">
        </dx:GridViewDataTextColumn>                            
        <dx:GridViewDataDateColumn FieldName="FromDatePlaced" VisibleIndex="10">
        </dx:GridViewDataDateColumn>
        <dx:GridViewDataTextColumn FieldName="ToPlacementStatus" ReadOnly="True" VisibleIndex="11">
        </dx:GridViewDataTextColumn>   
        ...
        ...           
    </Columns>

</dx:ASPxGridView>

<asp:Button ID="btnSearch" CssClass="btn btn-info" runat="server" Text="Search" OnClick="btnSearch_Click" />

选择一行的屏幕截图

select one row

服务器端代码

public string[] Status { get; set; }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        List<string[]> listField = new List<string[]>();
        listField =(List<string[]>)ASPxGridView1.GetSelectedFieldValues(Status); \\this line showing error

        \\btnSearch.Text = (string)Status.GetValue(0);
    }
  

错误消息:

     

无法将System.Collection.Generic.List类型转换为System.Collection.Generic.List

1 个答案:

答案 0 :(得分:2)

您必须将每个对象转换为string[]

List<string[]> listField = ASPxGridView1.GetSelectedFieldValues(Status)
    .Select(obj => (string[]) obj)
    .ToList();

List(T).ConvertAll

List<string[]> listField = ASPxGridView1.GetSelectedFieldValues(Status).ConvertAll(obj => (string[]) obj);