Can I manually add items to a repeater after binding it?

A

Alan Silver

Hello,

I have some data that I'm pulling from an SQL Server, that looks like...

1 jan 05 10
2 jan 05 7
4 jan 05 9
7 jan 05 12

I would like to display this in a repeater, but I want to fill in the
missing days with zeroes, so it would look like...

1 jan 05 10
2 jan 05 7
3 jan 05 0
4 jan 05 9
5 jan 05 0
6 jan 05 0
7 jan 05 12

I have two thoughts about this, either to add the missing rows to the
repeater after it has been bound, or to create a new data table and fill
it with the info from the one pulled from the db, including the missing
days as I go along, and bind the new data to the repeater.

Now the first seems easier, but I can't see any way to do it, although
that could be 'cos I haven't found it yet!!

I have an idea how to do it the second way, but it seems very messy.

Anyone any ideas? TIA
 
J

jasonkester

Your best solution would be to spin through the datatable and add
..NewRow()s as needed to fill in the holes before binding. LIfe will be
easier if you loop from the bottom to the top, since you won't need to
worry about jumping over the new rows as you insert them.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com
 
A

Alan Silver

Your best solution would be to spin through the datatable and add
.NewRow()s as needed to fill in the holes before binding. LIfe will be
easier if you loop from the bottom to the top, since you won't need to
worry about jumping over the new rows as you insert them.

Thanks for the suggestion. The way I have it at the moment (written
since posting) is that I pull out a DataReader and whizz through that,
populating a DataTable as I go. Along the way, I look for gaps and add
new rows as needed.

What's the easiest way to go through the DataTable itself? I was trying
to find out how to do this, but couldn't find a clear example. I'm sure
creating a new DataTable and copying the info over into it isn't very
efficient, but it was the only way I could work out how to do it.

Thanks
 
J

jasonkester

You can use a DataAdadapter to fill a table in a DataSet.

DataSet ds = new DataSet();

using (SqlConnection conn = YourConnectionFactory.GetConnection())
{
SqlCommand objComm = DBHelper.CreateStoredProc("YourStoredProcedure",
conn);
SqlDataAdapter adapt = new SqlDataAdapter(objComm);
adapt.Fill(ds, TableName);
conn.Close();
}

DataTable dt = ds.Tables[0];
for (int a=dt.Rows.Count-1; a>= 0; a--)
{
// check and insert as necessary
}

YourControl.DataSource = ds;
YourControl.DataBind();


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com
 
A

Alan Silver

Thanks Jason, that's useful to know.
You can use a DataAdadapter to fill a table in a DataSet.

DataSet ds = new DataSet();

using (SqlConnection conn = YourConnectionFactory.GetConnection())
{
SqlCommand objComm = DBHelper.CreateStoredProc("YourStoredProcedure",
conn);
SqlDataAdapter adapt = new SqlDataAdapter(objComm);
adapt.Fill(ds, TableName);
conn.Close();
}

DataTable dt = ds.Tables[0];
for (int a=dt.Rows.Count-1; a>= 0; a--)
{
// check and insert as necessary
}

YourControl.DataSource = ds;
YourControl.DataBind();


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top