Passing a dataRowView

J

John Hoge

I have a datagrid that needs to display information from a related
table, and I have use a template column to pass the Container.DataItem
object to a method called ShowPub as follows:

<asp:TemplateColumn HeaderText="Publisher">
<ItemTemplate>
<asp:Label Runat="server" Text='<%#ShowPub(Container.DataItem)%>' />
</ItemTemplate>
</asp:TemplateColumn>

The method ShowPub did not work when I first tried it like this:
protected string ShowPub(DataRowView drv)
{
EmpDS.employeeRow theRow = (EmpDS.employeeRow)drv.Row;
return theRow.publishersRow["pub_name"].ToString();
}

Once I modified it to take an object, and then cast it to a
DataRowView, it worked fine:

protected string ShowPub(object o)
{
DataRowView drv = (DataRowView)o;
EmpDS.employeeRow theRow = (EmpDS.employeeRow)drv.Row;
return theRow.publishersRow["pub_name"].ToString();
}

If I'm sending a DataRowView object, why can't I specifiy that type in
the method signature?
 
J

Jon Skeet [C# MVP]

John Hoge said:
I have a datagrid that needs to display information from a related
table, and I have use a template column to pass the Container.DataItem
object to a method called ShowPub as follows:

<asp:TemplateColumn HeaderText="Publisher">
<ItemTemplate>
<asp:Label Runat="server" Text='<%#ShowPub(Container.DataItem)%>' />
</ItemTemplate>
</asp:TemplateColumn>

The method ShowPub did not work when I first tried it like this:
protected string ShowPub(DataRowView drv)
{
EmpDS.employeeRow theRow = (EmpDS.employeeRow)drv.Row;
return theRow.publishersRow["pub_name"].ToString();
}

Once I modified it to take an object, and then cast it to a
DataRowView, it worked fine:

protected string ShowPub(object o)
{
DataRowView drv = (DataRowView)o;
EmpDS.employeeRow theRow = (EmpDS.employeeRow)drv.Row;
return theRow.publishersRow["pub_name"].ToString();
}

If I'm sending a DataRowView object, why can't I specifiy that type in
the method signature?

You can. What happened when you used the first version? If the compiler
was complaining that it didn't know that Container.DataItem would be a
DataRowView, then you just need to cast it:

ShowPub((DataRowView)Container.DataItem)
 
J

John Hoge

You can. What happened when you used the first version? If the compiler
was complaining that it didn't know that Container.DataItem would be a
DataRowView, then you just need to cast it:

ShowPub((DataRowView)Container.DataItem)


Jon,

I tried your second approach and that worked as well. So I guess this
means that the C# compiler does not know in advance that the
Coontainer.DataItem object is a DataRowView when referenced within a
Datagrid. Is it a good practice to explicitly cast all method
parameters whose type the compiler might be unaware of at compile
time?
 
J

Jon Skeet [C# MVP]

John Hoge said:
I tried your second approach and that worked as well. So I guess this
means that the C# compiler does not know in advance that the
Coontainer.DataItem object is a DataRowView when referenced within a
Datagrid. Is it a good practice to explicitly cast all method
parameters whose type the compiler might be unaware of at compile
time?

Cast when you have to, and only when you have to. In this case, the
Container.DataItem property is probably Object, so you need to tell the
compiler that at runtime it will actually be a DataRowView. In other
cases, you won't have to do that because the property will be of the
right type.

Unless there are two methods (eg Foo(object) and Foo(DataRowView)) and
you want to make sure that the more specific version is being called,
you can generally cast only when the compiler tells you to - although
after a fairly short amount of time you'll work out when that is and
won't have to wait for a compiler error.
 

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

Top