Probs in: Data grid calculated column Itemtemplate

R

Raghu Raman

Hi

i just want to show 2 fields in the datagrid one is the customerid &
other one is just the postal code field.I want to display the customer
id as first column and the postalcode is to be multiplied(calculated
column)by 2 as my second column while displaying.

my datagrid settings are
-------
<Columns><asp:BoundColumn DataField="customerid"
HeaderText="customerId"></asp:BoundColumn>
<asp:BoundColumn DataField="postalcode"
HeaderText="postalcode"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Values calculated">
<ItemTemplate><%#
Docals(DataBinder.Eval(Container.DataItem,"postalcode"))%>
</ItemTemplate> </asp:TemplateColumn></Columns>
-------
my c#.net procedure (for Docals)
-------------------------
public string Docals(string postalcode)
{return Convert.ToString(Convert.ToDouble(postalcode) * 2);
}
----------------------Error--
CS1502: The best overloaded method match for
'Endavour.Hiddencols_.Docals(string)' has some invalid arguments
*******Pls tell me where i went wrong.
Regards & Thanks
Raghu
 
K

Ken Cox [Microsoft MVP]

Raghu

It looks like your helper function doesn't know that it is getting a string,
so you have to force it to a string before you pass the value. See the code
below. (Warning: I'm not strong in C#)

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto



<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="customerid"
HeaderText="customerId"></asp:BoundColumn>
<asp:BoundColumn DataField="postalcode"
HeaderText="postalcode"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Calculated">
<ItemTemplate>
<%# Docals(
Convert.ToString(DataBinder.Eval(Container.DataItem,"postalcode")))%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


private void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.DataSource=CreateDataSource();
DataGrid1.DataBind();
}



public string Docals(string postalcode)
{
return Convert.ToString(Convert.ToDouble(postalcode) * 2);
}

ICollection CreateDataSource()
{

// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;

// Define the columns of the table.
dt.Columns.Add(new DataColumn("postalcode", typeof(String)));
dt.Columns.Add(new DataColumn("customerId", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(Double)));

// Populate the table with sample values.
for (int i=0; i<=10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

}
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top