This is a migrated thread and some comments may be shown as answers.

[Solved] Bind in RadGrid using DataTable

2 Answers 332 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos Eduardo Nogueira
Top achievements
Rank 1
Carlos Eduardo Nogueira asked on 13 Jul 2009, 02:33 PM
Hi,

I would like to generate columns in my RadGrid as I have in my DataTable. I created my DataTable about XML file. The structure of XML file you can see below:

<?xml version="1.0" encoding="utf-8" ?>
<Tabela>
  <Colunas>
    <Coluna NomeColuna="nome_completo" NomeCabecalho="Nome"></Coluna>
    <Coluna NomeColuna="matricula" NomeCabecalho="Registro"></Coluna>
    <Coluna NomeColuna="tipo_deficiencia" NomeCabecalho="Deficiência"></Coluna>
  </Colunas>
  <Linhas>
    <Linha NomeColuna="nome_completo" Conteudo="João"></Linha>
    <Linha NomeColuna="matricula" Conteudo="123456"></Linha>
    <Linha NomeColuna="tipo_deficiencia" Conteudo="Física"></Linha>
    <Linha NomeColuna="nome_completo" Conteudo="Maria"></Linha>
    <Linha NomeColuna="matricula" Conteudo="654321"></Linha>
    <Linha NomeColuna="tipo_deficiencia" Conteudo="Auditiva"></Linha>
  </Linhas>
</Tabela>

In a code aspx, I have the code in a button, as you can see below:

       try
        {
            System.Data.DataSet _dsDadosXML = new System.Data.DataSet();

            System.Data.DataTable _dtTabela = new System.Data.DataTable("Dados");
            
            System.Data.DataRow _drLinhas;

            _dsDadosXML.ReadXml(Server.MapPath("~/Protegidos/Empresas/Relatorios/XMLFile.xml"));
            System.Data.DataTable _dtTabelaXML = _dsDadosXML.Tables["Coluna"];
            foreach (System.Data.DataRow linha in _dtTabelaXML.Rows)
            {
                System.Data.DataColumn _dcColunas = new System.Data.DataColumn();

                _dcColunas.ColumnName = linha[0].ToString();
                _dcColunas.Caption = linha[1].ToString();
                _dtTabela.Columns.Add(_dcColunas);
            }

            _dtTabelaXML = _dsDadosXML.Tables["Linha"];
            foreach (System.Data.DataRow linha in _dtTabelaXML.Rows)
            {
                _drLinhas = _dtTabela.NewRow();
                _drLinhas[linha[0].ToString()] = linha[1].ToString();
                _dtTabela.Rows.Add(_drLinhas);
            }

            RadGrid1.DataSource = _dtTabela;
            RadGrid1.DataBind();
        }
        catch (Exception ex)
        {
            lblResultado.Text = ex.Message;
        }

And my RadGrid in source aspx is:

                 <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Telerik"
                            GridLines="None" AllowPaging="true" AllowSorting="true" Width="97%"
                            AutoGenerateColumns="false" ShowStatusBar="true"
                            PageSize="10">
                            <HeaderContextMenu Skin="WebBlue">
                                <CollapseAnimation Type="OutQuint" Duration="200" />
                            </HeaderContextMenu>
                            
                            <MasterTableView GridLines="None" Width="100%" CommandItemDisplay="None"
                                Font-Bold="false" Font-Italic="false" Font-Overline="false" Font-Strikeout="false"
                                Font-Underline="false" Name="TableViewTeste">
                                
                                <PagerStyle NextPageText="Próximo" PrevPageText="Anterior" />
                            </MasterTableView>
                            
                            <FilterMenu Skin="WebBlue">
                                <CollapseAnimation Type="InOutQuint" Duration="200" />
                            </FilterMenu>
                            <StatusBarSettings LoadingText="Aguarde..." ReadyText="Pronto" />
                        </telerik:RadGrid>

So, after to pass the DataBind method, I can't see anything in my RadGrid, that is, the data wasn't populated . Is there anything wrong with the code or other part?



2 Answers, 1 is accepted

Sort by
0
Carlos Eduardo Nogueira
Top achievements
Rank 1
answered on 13 Jul 2009, 03:25 PM
So, I would like to talk for you, if I put the same code to bind RadGrid no NeedDataSource method, I can see the data on my grid. Someone know the difference to put the code in this method and to put in button to bind the data on RadGrid?
0
Iana Tsolova
Telerik team
answered on 16 Jul 2009, 12:20 PM
Hello Carlos,

Try setting the AutoGenerateColumns property of the grid to true and see if it makes any difference.

Additional information on simple and advanced data-binding of RadGrid is available in the below articles:

http://www.telerik.com/help/aspnet-ajax/grdsimpledatabinding.html
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

I hope this helps.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Carlos Eduardo Nogueira
Top achievements
Rank 1
Answers by
Carlos Eduardo Nogueira
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or