cancel
Showing results for 
Search instead for 
Did you mean: 

How to modify cells in a table?

Former Member
0 Kudos

Hi,

This seems like a really stupid question but

how do I change a TableRows cells?

I have a Table, I have added some TableRows

TableRow tr = new TableRow();

tr = Table1.ItemTemplate;

<add cells>

Table1.Items.Add(tr);

in the bit in the middle I'd like to set some values but nothing I do works

Adding to the Cells adds columns to the table not cells to the row

Vin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Vin,

when you set tr to be the itemTemplate, from this point on you're actually working on the item template itself (reference, not copy..). So that's why adding cells adds rows.

Just work on the new row you created without using the item template. Also you'll need to make sure you have the right number of cells as the header. The item template is used either to edit the columns, or as an actual template when data binding is done (to copy from).

By the way - why do you do this in code? If you're just creating a manual table why not work with the designers ("edit columns" and "edit rows")?

Former Member
0 Kudos

I tried that and it didn't work, I assume I am still doing something stupid

my code is below

I am adding the rows dynamically

I will be querying a web service and getting a number of arrays back which I want to add to the table as rows and columns

Vin

TableRow tr = new TableRow();

TableCell c1 = new TableCell();

c1.Title = "c1";

tr.Cells.Add(c1);

TableCell c2 = new TableCell();

c2.Title = "c2";

tr.Cells.Add(c2);

TableCell c3 = new TableCell();

c3.Title = "c3";

tr.Cells.Add(c3);

Table1.Items.Add(tr);

Former Member
0 Kudos

OK. No, it's not something stupid. What you're trying to do here <i>is</i> a little tricky. I would recommend that you instead create an datasource from what you get from your web service (like an array or fill a DataGrid) and bind the table to it. It should be simpler and nicer... but anyway - you were close

I'm assuming you want to create the titles dynamically too...

So this code should work:

// Add the "titles"

TableCell t1 = new TableCell();

t1.Title = "t1";

TableCell t2 = new TableCell();

t2.Title = "t2";

Table1.ItemTemplate.Cells.Add(t1);

Table1.ItemTemplate.Cells.Add(t2);

// Add a row

TableRow tr = new TableRow();

// I know it's a little confusing, but in fact a real

// TableRow contains a collection of TableBodyCells.

// (TableCells are used only for the template):

TableBodyCell c1 = new TableBodyCell();

c1.TableCellContent = new TextView("c1");

// put in your cell any sap control (image, link..)

tr.Cells.Add(c1);

TableBodyCell c2 = new TableBodyCell();

c2.TableCellContent = new TextView("c2");

tr.Cells.Add(c2);

Table1.Items.Add(tr);

Goodluck, and let me know how it worked for you!

P.S: maybe later, when you're done, you can post some code samples from what you did... it sounds interesting.

Regards,

Ofer

Former Member
0 Kudos

That works perfectly.

Not sure how interesting it'll really be

Most of the code will be just the snippet you gave me there but I'll post some code when I have a working version anyway

Thanks, Vin

Former Member
0 Kudos

Here is the code I promised (whether anyone is still reading or not!)

Not sure how interesting it is!

The structure is that a database can contain systems, systems can contain batches

The user will select a database and system from a combo box

Then (under page load) this function will be called to use those selection and fill a table with batches and some of the batches properties

RAPIDWS is our web service

S88Batch and S88BatchFilter are structs available through the web service

Enjoy Vin

/////////////////////////////////////

// This code is indented but posting seems to trim lines so it'll be a bit hard to read

private void LoadBatches(string sDatabase, string sSystem, bool bUseSession)

{

// store the position in the table and then set it to zero before clearing

// this prevents the table from resizing to show a load of empty rows

int iFirstVisibleRow = tblBatches.FirstVisibleRow;

tblBatches.FirstVisibleRow = 0;

tblBatches.Items.Clear();

string sLastDatabase = (Session[m_csLastDatabaseBatch] == null) ? "" : (string)Session[m_csLastDatabaseBatch];

Session[m_csLastDatabaseBatch] = sDatabase;

// if we have changed databases throw the system away

if (sLastDatabase != sDatabase)

{

sSystem = "";

}

string sLastSystem = (Session[m_csLastSystems] == null) ? "" : (string)Session[m_csLastSystems];

Session[m_csLastSystems] = sSystem;

string[] asBatchIDs;

string[] asStartTimes;

string[] asEndTimes;

string[] asUniqueIDs;

// only get the batches from the server if we haven't a cached set, have changed database or system, or have been told to

// otherwise use the cached version

if ( (Session[m_csBatchIDs] == null) || (sLastDatabase != sDatabase) || (sLastSystem != sSystem)

|| (bUseSession == false) )

{

RAPIDWS.S88Batch[] aBatches;

try

{

aBatches = m_wsRapid.S88_GetBatches(sDatabase, sSystem, new RAPIDWS.S88BatchFilter());

}

catch(Exception)

{

// will happen if the user hasn't permissions to access the web service

return;

}

asBatchIDs = new string[aBatches.Length];

asStartTimes = new string[aBatches.Length];

asEndTimes = new string[aBatches.Length];

asUniqueIDs = new string[aBatches.Length];

for (int iBatch = 0; iBatch < aBatches.Length; ++iBatch)

{

asBatchIDs[iBatch] = aBatches[iBatch].sBatchId;

asStartTimes[iBatch] = aBatches[iBatch].dtStartTime.ToString();

asEndTimes[iBatch] = aBatches[iBatch].dtEndTime.ToString();

asUniqueIDs[iBatch] = aBatches[iBatch].sUniqueBatchId;

}

Session[m_csBatchIDs] = asBatchIDs;

Session[m_csStartTimes] = asStartTimes;

Session[m_csEndTimes] = asEndTimes;

Session[m_csUniqueIds] = asUniqueIDs;

}

else

{

asBatchIDs = (string[])Session[m_csBatchIDs];

asStartTimes = (string[])Session[m_csStartTimes];

asEndTimes = (string[])Session[m_csEndTimes];

asUniqueIDs = (string[])Session[m_csUniqueIds];

}

for (int i = 0; i < asBatchIDs.Length; ++i)

{

TableRow tr = new TableRow();

TableBodyCell c1 = new TableBodyCell(tblBatches, tr, 0);

c1.TableCellContent = new TextView(asBatchIDs<i>);

tr.Cells.Add(c1);

TableBodyCell c2 = new TableBodyCell(tblBatches, tr, 1);

c2.TableCellContent = new TextView(asStartTimes<i>);

tr.Cells.Add(c2);

TableBodyCell c3 = new TableBodyCell(tblBatches, tr, 2);

c3.TableCellContent = new TextView(asEndTimes<i>);

tr.Cells.Add(c3);

tr.Key = asUniqueIDs<i>;

tblBatches.Items.Add(tr);

}

// restore the table position if possible

if (iFirstVisibleRow <= tblBatches.Items.Count)

{

tblBatches.FirstVisibleRow = iFirstVisibleRow;

}

}

Former Member
0 Kudos

This is great, Thanks

Ofer

Former Member
0 Kudos

How to update data if its in a textbox? How should I call the RFC?

Thank you.

regards.

reiner_hille-doering
Active Contributor
0 Kudos

Having edit-enabled controls in a Table (in ASP.NET) is usually complicated because ASP.NET databinding is only one-way. If you do so, you need to write your on PostbackDataHandlers to get the result from you edit fiedlds that you created in the table. Then you need to take there content to bring it somehow to your RFCs.

Former Member
0 Kudos

Have you filled the PDK for .NET survey yet? You can win an MP3 player!!!

http://feedback.sap.com/efs/vote?campaign=39c7899ecca40a6709a638bd0388ac8b&org=332

Answers (0)