1

I am trying to do the following but with a radgrid, that does not have a Rows attribute. How can i do the equivalent?

Thanks

DataTable dt = new DataTable();    
for (int j = 0; j < grdList.Rows.Count; j++)
{

    DataRow dr;
    GridViewRow row = grdList.Rows[j];
    dr = dt.NewRow();
    for (int i = 0; i < row.Cells.Count; i++)
    {
        dr[i] = row.Cells[i].Text;
    }

    dt.Rows.Add(dr);
}
4

1 回答 1

3

请尝试以下代码片段。

.aspx

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound"
        OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" AllowFilteringByColumn="true"
        EnableLinqExpressions="false">
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

.aspx.cs

 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
          new { ID = 1, Name ="aaa"},
          new { ID = 2, Name = "bbb"},
          new { ID = 3, Name = "ccc"},
          new { ID = 4, Name = "ddd"},
           new { ID = 5, Name ="eee"}
        };
        RadGrid1.DataSource = data;
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        int columncount = 0;

        foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
        {
            if (!string.IsNullOrEmpty(column.UniqueName) && !string.IsNullOrEmpty(column.HeaderText))
            {
                columncount++;
                dt.Columns.Add(column.UniqueName, typeof(string));
            }
        }

        DataRow dr;
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            dr = dt.NewRow();

            for (int i = 0; i < columncount; i++)
            {
                dr[i] = item[RadGrid1.MasterTableView.Columns[i].UniqueName].Text;
            }

            dt.Rows.Add(dr);
        }

        // Access Your DataTable
    }

用VB代码。

.aspx

<div>
 <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnItemDataBound="RadGrid1_ItemDataBound"
        OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" AllowFilteringByColumn="true"
        EnableLinqExpressions="false">
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Generate Table" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="check Table" OnClick="Button2_Click" />
    <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False">
        <MasterTableView>
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <br />
</div>

.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    'Stopwatch sw = Stopwatch.StartNew();
    'string str = sw.Elapsed.Seconds.ToString();
    'sw.Stop();

    If Not IsPostBack Then
        Session("GridTable") = Nothing
    End If


End Sub

Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As    GridNeedDataSourceEventArgs)
    Dim dt As New DataTable()
    dt.Columns.Add("ID", GetType(Integer))
    dt.Columns.Add("Name", GetType(String))
    dt.Rows.Add(1, "Name1")
    dt.Rows.Add(2, "Name2")
    dt.Rows.Add(3, "Name3")
    dt.Rows.Add(4, "Name4")
    RadGrid1.DataSource = dt
End Sub

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As New DataTable()

    Dim columncount As Integer = 0

    For Each column As GridColumn In RadGrid1.MasterTableView.Columns
        If Not String.IsNullOrEmpty(column.UniqueName) AndAlso Not String.IsNullOrEmpty(column.HeaderText) Then
            columncount += 1
            dt.Columns.Add(column.UniqueName, GetType(String))
        End If
    Next

    Dim dr As DataRow
    For Each item As GridDataItem In RadGrid1.MasterTableView.Items
        dr = dt.NewRow()

        For i As Integer = 0 To columncount - 1
            dr(i) = item(RadGrid1.MasterTableView.Columns(i).UniqueName).Text
        Next

        dt.Rows.Add(dr)
    Next

    Session("GridTable") = dt
    ' Access Your DataTable
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    If Session("GridTable") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(Session("GridTable"), DataTable)
        RadGrid2.DataSource = dt
        RadGrid2.DataBind()
    End If
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik, @toddanglin
'Facebook: facebook.com/telerik
'=======================================================
于 2012-08-24T18:10:42.310 回答