【问题标题】:C# read data from gridView to stringC#从gridView读取数据到字符串
【发布时间】:2010-12-05 01:16:01
【问题描述】:

嘿,我有一个 gridView,我想提取 PhotoPath (ftppath + 文件名 (ftp://192.168.1.2/Jelly.jpg))

它在 asp.net 中,我不确定如何检索数据,我设置了 gridView 以供选择,我喜欢“根据”选择将 PhotoPath 存储在一个字符串中。

来源

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="StudentID" 
        DataSourceID="SqlDataSource1" Height="263px" Width="915px" 
        AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="No" HeaderText="MatricNo" 
                SortExpression="MatricNo" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="Current" HeaderText="CurrentModules" 
                SortExpression="CurrentModules" />
            <asp:BoundField DataField="PhotoPath" HeaderText="PhotoPath" 
                SortExpression="PhotoPath" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" BorderColor="Blue" BorderStyle="Solid" 
            Font-Size="Small" />
        <SelectedRowStyle BackColor="#D1DDF1" BorderStyle="Solid" Font-Bold="True" 
            Font-Size="Small" ForeColor="#333333" HorizontalAlign="Center" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

我试过这个:

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string PhotoPath;
        GridView1.CurrentRow.Cells[0].Value.ToString();
    }

但当前行不是定义的一部分。

【问题讨论】:

    标签: c# .net asp.net gridview


    【解决方案1】:

    试试

    protected void GridView1_SelectedIndexChanging(object sender, EventArgs e) {
        string PhotoPath = "";
        // Get the currently selected row. Because the SelectedIndexChanging event
        // occurs before the select operation in the GridView control, the
        // SelectedRow property cannot be used. Instead, use the Rows collection
        // and the NewSelectedIndex property of the e argument passed to this 
        // event handler.
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
    
        PhotoPath = row.Cells[5].Text;
    }
    

    或

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
        string PhotoPath = "";
        GridViewRow row = GridView1.SelectedRow;
    
        PhotoPath = row.Cells[5].Text;
    }
    

    【讨论】:

    • 非常感谢 :) 在 NewSelectedIndex 出现一个小错误,但错误 1“System.EventArgs”不包含“NewSelectedIndex”的定义,并且没有扩展方法“NewSelectedIndex”接受“System”类型的第一个参数可以找到 .EventArgs'(您是否缺少 using 指令或程序集引用?)
    • 要获取新选择的行的索引,您需要处理 SelectedIndexChanging 事件。那么 e 应该有 NewSelectedIndex 属性。
    • 哎呀!你是对的!对此感到抱歉。我已经相应地更新了我的答案
    • 是的,没关系:)也许你可以看看我的最新帖子,非常令人不安,哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多