GridView

  • Thread starter Thread starter ata
  • Start date Start date
A

ata

Hi folks,
Consider a gridview that's being bound to a ObjectDataSource that uses
an adapter to handle data access logic.

1. How am I supposed to have a RowIndex column *without* modifying the
Entity class?
2. How am I supposed to assign a given TAG object to each row?

Any help would be highly appreciated,

Thanks
Jack
 
1. How am I supposed to have a RowIndex column *without* modifying the
Entity class?

Firstly, you're not "supposed to" have a RowIndex column - there's no law
that says you have to have one...

However, it's easy enough:

1) Add a template field to your existing GridView:

<asp:TemplateField HeaderText="RowIndex">
<ItemTemplate>
<asp:Label ID="lblRowIndws" runat="server" />
</ItemTemplate>
</asp:TemplateField>

2) Wire up a RowDataBound event to your GridView if it doesn't already have
one:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gri...

3) Add the following code to the RowDataBound event:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = e.Row.RowIndex.ToString();
//e.Row.Cells[6].Text = (Convert.ToInt32(e.Row.RowIndex) +
1).ToString();
}

}

Remember to modify the cell indexer accordingly...
2. How am I supposed to assign a given TAG object to each row?

Can you please explain what you mean by "given TAG object"...?

I need to store the Entity Object (for future references) associated
with each row.
 
And did my response not work for you...?

Of course, the first problem (RowIndex) will be solved as you
described.
However, I still need to store an Object for every single row. How's
this possible?
 
What sort of object is it? What does it look like? Is it plain text? If not,
can it be serialised?
Remember that a GridView is just an HTML table by the time it gets rendered
to the browser...

Is it not possible to retrieve this object in some other way so that you
don't need to actually squirt it down to the browser? Maybe via the
Gridview's underlying data source...?

Well, let's say it's an Employee object, that can be actually binary
serialized (because of different circumstances I've got).
I need to have the actual object graph that's being associated with
each row for update/delete operations.

As far as I understood, I've to serialize the object graph to base-64,
and keep it as an invisible row info.
That's ok so far.

However, I've got no idea how to retrieve the base-64 string back when
deleting/updating a given record.

Any help would be highly appreciated,

Thanks a lot.
 
But why do you need to actually send the entire serialised Employee object
down to the client browser for this? What purpose can it possibly serve
there...? You're presumably using a postback to do the update and delete
operations, no...? Is each Employee not identified by a unique identifier?

Of course, an employee record is identified by a single Identifier.
But we've got a mechanism to control the concurrency which actually
forces me to send back the object graph to the service layer, either
for modification or for deletion.

Is this applicable? Thanks.
 
But I *still* can't see why you need to store the entire object
client-side... :-)

Why can't you just store the unique Employee identifier? Then, during
postback, retrieve the unique Employee identifier from the row being
modified, construct an Employee object out of it, and then do whatever you
need to do with it...

Well, it takes a long time to describe things. Would you please
let me know how to access an invisible cell value, while removing a
given row?
 
Depends how you "hide" it... If you set a column's Visible property to
false, then it won't even get rendered to the client browser, so that won't
help you here.

However, a much better method involves the DataKeys property of the
DataGrid - set it to the Employee ID and then you can retrieve it easily on
postback:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gri...

OK, I'm convinced that your solution is really reasonable. So I'm
trying to fix
my thoughts... I've just set the DataKeyNames to Id and ChangeStamp as
you
already mentioned.

I'm trying to remove the record by it's ID/ChangeStamp pairs, however,
the DeleteEmployee
method gets called twice and it's really annoying. The first time,
it's actually removed, I swear!
So why the runtime system calls the function for the second time? :)

Thank you really for your time
Jack
 
1. How am I supposed to have a RowIndex column *without* modifying the
Entity class?

Firstly, you're not "supposed to" have a RowIndex column - there's no law
that says you have to have one...

However, it's easy enough:

1) Add a template field to your existing GridView:

<asp:TemplateField HeaderText="RowIndex">
<ItemTemplate>
<asp:Label ID="lblRowIndws" runat="server" />
</ItemTemplate>
</asp:TemplateField>

2) Wire up a RowDataBound event to your GridView if it doesn't already have
one:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gri...

3) Add the following code to the RowDataBound event:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = e.Row.RowIndex.ToString();
//e.Row.Cells[6].Text = (Convert.ToInt32(e.Row.RowIndex) +
1).ToString();
}

}

Remember to modify the cell indexer accordingly...
2. How am I supposed to assign a given TAG object to each row?

Can you please explain what you mean by "given TAG object"...?

Back to the original question and the solution you've just provided.
I've noticed that when I press the save button, that's being placed
somewhere
outside the gridview control (which causes the postback and hence the
submit behavior),
the row# column doesn't get updated. I've traced the code and I
noticed that the
gridView_RowDataBound function doesn't get called for DataRow row
types!!!

Would you please let me know what's going on wrong and how am I
supposed to fix this?

Thanks
 
Back
Top