从GridView调用数据

时间:2012-08-13 08:04:28

标签: asp.net .net vb.net datagrid

我有一个GridView和一堆像这样的DynamicField;

<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource">
<Columns>
<asp:DynamicField HeaderText="Date Submitted" DataField="DATE_CREATED" />
<asp:DynamicField HeaderText="Assigned To" DataField="ASSIGNED_TO" />
<asp:DynamicField HeaderText="Active Account" DataField="Active_Account" />
<asp:DynamicField HeaderText="Client ID" DataField="CLIENT_ID" />
<asp:DynamicField HeaderText="Client Type" DataField="CLIENT_Type" />
</Columns>
</asp:GridView>

<asp:EntityDataSource ID="GridDataSource" OnSelected="TotalRows" runat="server" 
EnableDelete="true">
    <WhereParameters>
        <asp:DynamicControlParameter ControlID="FilterRepeater" />
    </WhereParameters>
</asp:EntityDataSource>

现在它显示正常,正确的数据进入屏幕但是当我尝试使用行号访问该数据时,我总是找到空白单元格。例如,我尝试以下方法检查每个单元格,但无济于事;

Dim x As Integer = 0
    Dim y As Integer = 0
    While x < GridView1.Rows.Count
        While y < GridView1.Rows(x).Cells.Count
            If Not (GridView1.Rows(x).Cells(y).Text = "") Then
                MsgBox(String.Format("{0},{1},{2}", x.ToString, y.ToString, 
                      GridView1.Rows(x).Cells(y).Text))
            End If
            y = y + 1
        End While
        x = x + 1
        y = 0
    End While

没有显示消息框,因此所有单元格都是空字符串。但我可以清楚地看到他们已经在屏幕上填充!我做错了什么?

更新

在Pilgerstorfer Franz建议我查找Textbox元素后,我使用了以下代码,它基本上查看了表中的所有单元格并尝试从中提取数据,如果它不是空白,则显示一个msgbox(也告诉我任何我没有考虑的新控件);

If GridView1.Rows(0).RowType = DataControlRowType.DataRow Then
    For r = 0 To GridView1.Rows.Count - 1
        For c = 0 To (GridView1.Rows(r).Cells.Count - 1)
            Dim cell = GridView1.Rows(r).Cells(c)

            For b = 0 To cell.Controls.Count - 1
                If (cell.Controls(b).GetType() Is GetType(TextBox)) Then
                    Dim td = CType(cell.Controls(b), TextBox)
                    Text = td.Text.Trim
                ElseIf (cell.Controls(b).GetType() Is GetType(LiteralControl)) Then
                    Dim td = CType(cell.Controls(b), LiteralControl)
                    Text = td.Text.Trim
                ElseIf (cell.Controls(b).GetType() Is GetType(DynamicControl)) Then
                    Dim td = CType(cell.Controls(b), DynamicControl)
                    Text = td.Table.Columns.Item(c).DisplayName()
                Else
                    MsgBox(String.Format("New Control of type: {0}", cell.Controls(b).GetType().FullName))
                End If
                If Not Text = "" Then
                    MsgBox(String.Format("{0},{1},{2}", c.ToString, b.ToString, Text))
                End If
            Next
        Next
    Next
End If

不幸的是,大多数单元格只包含DynamicControl,因为我将DisplayName放入Text,我每次只获取列标题。那么如何从DynamicControl中获取文本值属性?

其他信息

我对此问题更加困惑,因为这是一个我正在更新的项目,其中有两行初始代码;

Dim UserID = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)
Dim clientType As String = GridView1.DataKeys(e.RowIndex).Item(1).ToString

这些成功带回了UserID和ClientType。现在我并不真正理解DataKeys,但我尝试使用;

Dim clientType As String = GridView1.DataKeys(e.RowIndex).Item(Num).ToString

其中Num每次增加一个期望它带回其余的行数据,但我只是得到一个索引超出范围错误。

另一个更新!!

这是ASPX页面的另一部分,但我并不完全确定它的作用。 Pilgerstorfer Franz创建了一些代码来查找dynamicControl创建的文本框。现在我想知道这里的代码是否导致使用其他类型的控件而不是Textboxes。想法?

<asp:FilterRepeater ID="FilterRepeater" runat="server" Visible="false">
            <ItemTemplate>
                <h2><asp:Label ID="lblDisplay" runat="server" Text='<%#
Eval("DisplayName") %>' AssociatedControlID="DynamicFilter$DropDownList1" /></h2>
                <asp:DynamicFilter runat="server" ID="DynamicFilter" 
OnSelectedIndexChanged="OnFilterSelectedIndexChanged" />
            </ItemTemplate>
            <FooterTemplate><br /><br /></FooterTemplate>
        </asp:FilterRepeater>

2 个答案:

答案 0 :(得分:0)

根据我的假设(见评论)我做了一个小小的演示...

<asp:DynamicDataManager ID="DynamicDataManager1" runat="server">
        <DataControls>
            <asp:DataControlReference ControlID="GridView1" />
        </DataControls>
</asp:DynamicDataManager>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataKeyNames="ContactID" DataSourceID="EntityDataSource1" AllowPaging="true" 
       OnRowUpdating="GridView1_RowUpdating">
        <Columns>                
            <asp:DynamicField DataField="ContactID" ReadOnly="true" HeaderText="ContactID" />
            <asp:DynamicField DataField="Title" HeaderText="Title" />
            <asp:DynamicField DataField="FirstName" HeaderText="FirstName" />
            <asp:DynamicField DataField="LastName" HeaderText="LastName" />
            <asp:CommandField ShowEditButton="True" />
        </Columns>
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=dbTestIT34_EFEntities"
        DefaultContainerName="dbTestIT34_EFEntities" EnableFlattening="False" EnableUpdate="True"
        EntitySetName="Contacts" EntityTypeFilter="Contact">
</asp:EntityDataSource>

根据我的数据存储,我确实处理了rowUpdating事件。不幸地访问动态控件中的任何控件并不容易。所以我做了一个非常快速和肮脏的黑客......

protected void Page_Init(object sender, EventArgs e)
{
    GridView1.EnableDynamicData(typeof(Contact));
    EntityDataSource1.ContextType = typeof(dbTestIT34_EFEntities);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int row = 0;
    int firstNameColumn = 2;
    while (row < GridView1.Rows.Count)
    {
        // sorry, a little bit confusing ...
        //     as dynamic control generates other controls dynamically, 
        //     this was the very first thing that worked for me!
        TextBox t = GridView1.Rows[row].Cells[firstNameColumn].Controls[0].Controls[0].Controls[0] as TextBox;
        if (t != null)
            Debug.WriteLine(t.Text);

        row++;
    }
}

希望有所帮助!

答案 1 :(得分:0)

通过控件迭代从来没有对我有用,所以最后我放弃了,只是再次询问了数据库......呃它有效。