Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im trying to split the results of my dataset between two repeaters, displaying all the even in the left repeater and the odd index in the right. so far my code is as follows.
C#
CoachFavPlayerProvider coachFavPlayer = new CoachFavPlayerProvider();
DataSet dataset = coachFavPlayer.CoachFavPlayer(CoachID);

DataTable leftContent = dataset.Tables[0];
DataTable rightContent = dataset.Tables[0]; 
    
favRepeater.DataSource = rightContent;
favRepeaterRight.DataSource = leftContent;

favRepeaterRight.DataBind();
favRepeater.DataBind();

this obviously just binds the whole lot to both repeaters, and i cant for the life of me figure out how to split it. thanks in advance! :)
Posted
Updated 28-May-13 19:49pm
v2

filter datatbles before assin to datasource of repeater control
C#
 DataTable leftContent = dataset.Tables[0]
leftContent.DefaultView.RowFilter = "(SrNo % 2) = 0";
 
 DataTable rightContent = dataset.Tables[0]
RightContent.DefaultView.RowFilter = "(SrNo % 2) = 1";


or
C#
DataTable leftContent = dataset.Tables[0].Clone();
           for(i=0;i<dataset.Tables[0].rows.count/2 ; i++)
           {

               dtTemp.ImportRow(dataset.Tables[0].rows[i]);
           }
DataTable rightContent = dataset.Tables[0].Clone();
           for(i=dataset.Tables[0].rows.count/2;i<dataset.Tables[0].rows.count ; i++)
           {

               dtTemp.ImportRow(dataset.Tables[0].rows[i]);
           }


Happy Coding!
:)
   
v3
Comments
nebiam 29-May-13 2:03am
   
Thanks for the quick response, is there a way of not specifying a row name?? the provided solution gives me the error: "Cannot find column [SrNo]" .. I just need to use the rowIndex or similar?
Aarti Meswania 29-May-13 2:08am
   
replace srno with column name which you want to mod by 2 as you described in question
nebiam 29-May-13 2:11am
   
sorry i may have no been clear, i dont have a column name to split the results, i just want to divide the results evenly between the repeaters. is there an item index i can use instead of column name?
Aarti Meswania 29-May-13 2:19am
   
then...
DataTable leftContent = dataset.Tables[0].Clone();
for(i=0;i
Aarti Meswania 29-May-13 2:20am
   
see updated solution
_Amy 29-May-13 2:19am
   
These are the real facts about your question, which you could have added in the question while asking.
_Amy 29-May-13 2:20am
   
Then check for the rowcount and split your datatable into two and then bind your repeater.
Here is the code tested by me and running fine.

Make a independent method to filter records as

C#
private DataTable GetFilteredData(DataTable sourceDataTable, string columnName, string value)
 {
     DataTable dtTemp = sourceDataTable.Clone();
     DataRow[] result = sourceDataTable.Select(columnName + " = '" + value + "'");
     foreach (DataRow row in result)
     {

         dtTemp.ImportRow(row);
     }
     return dtTemp;
 }


After this call this method into your function as

C#
CoachFavPlayerProvider coachFavPlayer = new CoachFavPlayerProvider();
DataSet dataset = coachFavPlayer.CoachFavPlayer(CoachID);
 
DataTable leftContent = GetFilteredData(dataset.Tables[0],"(SrNo % 2)","0");
DataTable rightContent = GetFilteredData(dataset.Tables[0],"(SrNo % 2)","1");

favRepeater.DataSource = rightContent;
favRepeaterRight.DataSource = leftContent;
 
favRepeaterRight.DataBind();
favRepeater.DataBind();


Hope this will help you.
   
Comments
nebiam 29-May-13 2:02am
   
Thanks for the quick response, is there a way of not specifying a row name?? the provided solution gives me the error: "Cannot find column [SrNo]" .. I just need to use the rowIndex or similar?
[no name] 29-May-13 8:30am
   
Ya it will give you an error until you put this extra column into your select statement.

put it as

select ROW_NUMBER ( ) OVER ( Your_Any_Primary_Key_Column_Name ORDER BY ASC/DESC ) AS SrNo,.......
You can use Select[^] function to filter your DataTable. See an example:
C#
DataRow[] dr1 = dataset.Tables[0].Select("(ColumnName % 2) = 0");
DataTable dt1 = dr1.CopyToDataTable();

DataRow[] dr2 = dataset.Tables[0].Select("(ColumnName % 2) = 1");
DataTable dt2 = dr2.CopyToDataTable();

And then you can set the datasource of repeater control. See this:
C#
favRepeater.DataSource = dt1;
favRepeaterRight.DataSource = dt2;

favRepeaterRight.DataBind();
favRepeater.DataBind();


I hope you understood the solution.
--Amit
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900