C# .NET - bind datarow[] in datatable in c#

Asked By muthuraman alexander
25-Aug-11 02:48 AM
Hi All,

Here i have a datarow collection like
DataRow[] drTemp =dtSample.Select("ID=100");
currently i binding this datarow collection  in to a Table by using foreach like
foreach(datarow dr in drTemp)
{
dtTemp.importRows(dr);
}


My Need is ............................................................................
With out using the Foreach how can i do this directly
i tried dtTemp.rows.add(drTemp);
but it was not working and showing some error

please any body help me for this task

thanks in advance
Muthu

  Ravi S replied to muthuraman alexander
25-Aug-11 02:50 AM
hi

Why not iterate through your DataRow array and add (using DataRow.ImportRow, if necessary, to get a copy of the DataRow), something like:

foreach (DataRow row in rowArray) {
   dataTable.ImportRow(row);
}

Make sure your dataTable has the same schema as the DataRows in your DataRow array.

  muthuraman alexander replied to Ravi S
25-Aug-11 02:51 AM
no i can use foreach
becas of some performance issues...
i hav to bind it directly ..
how can i ?



thanks in advance
Muthu
  Anoop S replied to muthuraman alexander
25-Aug-11 03:03 AM
Add DataRow to asp:DataTable

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
   <title>List Control Example</title>
   <script runat="server">
    Sub Page_Load()
     MyRepeater.DataSource = CreateData()
     MyRepeater.DataBind()
    End Sub
    Function CreateData() As DataTable
     Dim DT As New DataTable()
     Dim Row1, Row2, Row3, Row4 As DataRow
     DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
     DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
     Row1 = DT.NewRow()
     Row1("name""Name Here"
     Row1("city""City Here"
     DT.Rows.Add(Row1)
     Row2 = DT.NewRow()
     Row2("name""Another Name Here"
     Row2("city""City 2 Here"
     DT.Rows.Add(Row2)
     Row3 = DT.NewRow()
     Row3("name""Name 3"
     Row3("city""City 3"
     DT.Rows.Add(Row3)
     Row4 = DT.NewRow()
     Row4("name""Name 4"
     Row4("city""City 4"
     DT.Rows.Add(Row4)
     Return DT
    End Function
   </script>
</head>
<body>
   <h1>Control Templates Example</h1>
   <form runat="server">
    <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
     <asp:tablerow runat="server">
      <asp:tablecell runat="server">
         Repeater Control:
      </asp:tablecell>
      <asp:tablecell runat="server">
         <asp:repeater id="MyRepeater" runat="server">
          <headertemplate>
           <h3>Famous Composers' Birthplaces</h3>
           <table cellpadding="5" cellspacing="0">
            <tr>
               <td>Name<hr/></td>
               <td>City<hr/></td>
            </tr>
          </headertemplate>
          <itemtemplate>
            <tr>
               <td><strong><%# DataBinder.Eval(Container.DataItem, "name"%><strong></td>
               <td><%# DataBinder.Eval(Container.DataItem, "city"%></td>
            </tr>
          </itemtemplate>
          <footertemplate>
           </table>
          </footertemplate>
         </asp:repeater>
      </asp:tablecell>
     </asp:tablerow>
    </asp:table>
   </form>
</body>
</html>

http://www.java2s.com/Code/ASP/Data-Binding/AddDataRowtoaspDataTable.htm
  dipa ahuja replied to muthuraman alexander
25-Aug-11 03:04 AM
Without loop you cannot import all rows, The purpose of loop is this , that is perform same task mulitiple times

so it will loop and perform same task that is importing rows
  muthuraman alexander replied to dipa ahuja
25-Aug-11 04:59 AM
Thank you every one who replied and try to help me a lot
But i tried this

DataRow[] dr = dtSample.Select("ID=100");
DataTable dtFinal
= dr.copyToDataTable();


it worked great for me

Thanks
Muthu
Create New Account
help
bind a gridview to a datatable I need to bind a gridview to a datatable following is my code behind: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ProgressTable As DataTable ProgressTable = New DataTable("ProgressGrid") Dim Row1, Row2, Row3, Row4, Row5, Row6 As DataRow Try Dim REGION As DataColumn = New DataColumn("REGION") REGION.DataType = System.Type.GetType("System.String") ProgressTable.Columns.Add(REGION) Dim TOTALP3028 As DataColumn = New DataColumn("TOTALP3028") TOTALP3028.DataType = System.Type.GetType("System.Int32") ProgressTable.Columns.Add(TOTALP3028) Dim COMPLETED As
insert data into SQL 2005 from a text file that I parse. I build a DataTable: [CODE] public DataTable tbl = new DataTable(); public DataTable buildType4DataTable() { DataColumn loadTransactionCode = new DataColumn(); loadTransactionCode.ColumnName = "loadTransactionCode"; tbl.Columns.Add(loadTransactionCode); DataColumn companyID = new DataColumn(); companyID.ColumnName = "companyID"; tbl.Columns.Add(companyID); DataColumn cardholderIdentification = new DataColumn(); cardholderIdentification.ColumnName = "cardholderIdentification"; tbl
View state Hi , New to view state , and here is the issue : DataTable dtz = ViewState["GridWindowsService"]as DataTable; DataTable dt = new DataTable(); dt = dtz; dt.Rows.Add(dt.NewRow()); ViewState["GridWindowsService"] = dtz; dtz table is also having missing something ??? What you want to perform with view STate?? i want to set a datatable equal to the view state ( in this case it would be null datatset ) in which footer when there is no data in the grid ) I show you the simple example: DataTable DataTableA; protected void Page_Load( object sender, EventArgs e) { DataTableA = new DataTable (); DataTableA.Columns.Add( "Quantity" , typeof ( string )); DataTableA.Columns.Add( "Duration" , typeof ( string )); DataTableA.Rows.Add dipa" , "ahuja" ); DataTableA.Rows.Add( "sita" , "Ram" ); ViewState[ "dt" ] = DataTableA; if (!IsPostBack) { / / Datasource as dataTable GridView1.DataSource = DataTableA; GridView1.DataBind(); } } protected void Button1_Click( object sender, EventArgs e) { / / Adding new row
Creating datatables 0 down vote favorite I've a DataTable[having 5 columns] from db which has combined data. I need to invoke a group code: string colName = "ACCOUNT_ID" ; var allRows = combinedTable . AsEnumerable (); var accountRowGroups = allRows . GroupBy ( row = > row [ colName ]); DataTable masterDataTable = new DataTable (); DataTable childPricesDataTable = new DataTable (); / / Create the columns DataColumnCollection pdCols = combinedTable . Columns ; for ( int ndx = 0 ; ndx < pdCols . Count ; ndx masterDataTable . Columns . Add ( columnName , type ); childPricesDataTable . Columns . Add ( columnName , type ); } HI try this first create DataTable object. Create Columns for it and add in datatable. then add rows in the datatable. - -- -- - / / - -create datatable with 'No' & 'Name' column. DataTable dt = new DataTable (); / / - - Create datatable object DataColumn cl
How to convert datalist to datatable How to convert datalist to datatable without use foreach, forloop If you don't want to use loop than just try to get Binding datasource of DataList into Datatable as follows DataTable dt = (DataTable ) dlName.DataSource; hope this helps your hi, here gives you one link for solution: click here Regards, Way 1 void ToDatatable() { / / creating datatable DataTable dt = new DataTable (); / / adding column dt.Columns.Add( "id" , typeof ( int )); dt.Columns.Add( "name" , typeof ( string )); foreach