How to call C# method from itemTemplate gridview

3.4k views Asked by At

I have gridview with 3 columns. I need to add one more column 'Share' to this gridview. Query is there is no 'Share' column in database , we are calculating it through C# method. I had method named SHARE(), here how can i pass the value of Share to itemTemplate lable.

    <Columns>
        <asp:BoundField DataField="MarketName" HeaderStyle-Width="60px"  HeaderText="Market" />
        <asp:BoundField DataField="Year" HeaderStyle-Width="60px" HeaderText="Year" />
        <asp:BoundField DataField="Type" HeaderStyle-Width="60px" HeaderText="Type" />
        <asp:BoundField DataField= "TotalVolume" HeaderStyle-Width="100px" HeaderText= "Total Volume" />
        <asp:TemplateField>
        <HeaderTemplate>
          <asp:Label ID="lable1" runat="server" Width="60px" Text="Share"></asp:Label>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label ID="lblShare" runat="server" Text='<% #Share() %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>

Code:

    public void share()
    {
        float share = 0;
        int tot = 0;
        int vol = 0;
        c.cmd.CommandText = "Select * from MarketDetail";
        c.adp.Fill(c.ds,"vt");
        foreach (DataRow dr1 in c.ds.Tables["vt"].Rows)
        {
            tot = tot + Convert.ToInt32(dr1["TotalVolume"]);
        }
        foreach (DataRow dr2 in c.ds.Tables["vt"].Rows)
        {
            share = tot /Convert.ToInt32(dr2["TotalVolume"]);
          //how assign this 'Share' value to lable in grid //

        }

    }
4

There are 4 answers

0
anshul On BEST ANSWER

Thanks all friends. Now my code is working fine. firstly my method returns single value (ie. last one) while I need to print for every totalValue. Here is my working code:

<ItemTemplate>
  <asp:Label ID="lblShare" runat="server" Text='<%#share((Int32)Eval("TotalVolume"))%>'/>
 </ItemTemplate>

and

   public string share(int tv)
   {
    float share = 0;
    float tot = 0;
    c.cmd.CommandText = "Select * from MarketDetail";
    c.adp.Fill(c.ds, "vt");
    foreach (DataRow dr1 in c.ds.Tables["vt"].Rows)
    {
        tot = tot + Convert.ToInt32(dr1["TotalVolume"]);
    }        
    share = tot /tv ;
    c.ds.Tables["vt"].Clear();
    return share.ToString() +"%";
}
0
Manoj On

Your method should be return type method but you declare it 'void` type.

Like this...

    public float share()
    {
        float share = 0;
        int tot = 0;
        int vol = 0;
        c.cmd.CommandText = "Select * from MarketDetail";
        c.adp.Fill(c.ds,"vt");
        foreach (DataRow dr1 in c.ds.Tables["vt"].Rows)
        {
            tot = tot + Convert.ToInt32(dr1["TotalVolume"]);
        }
        foreach (DataRow dr2 in c.ds.Tables["vt"].Rows)
        {
            share = tot /Convert.ToInt32(dr2["TotalVolume"]);
          //how assign this 'Share' value to lable in grid //

        }
        return share;

    }
1
yogi970 On

Why you are using the server side code for this.You can do it by SQL alone.

Just add a extra field in your like

Select *,sum(TotalVolume) / TotalVolume as Share from MarketDetail;

Now i think you understand what i am saying.

And you can use this for getting value in label.

<ItemTemplate>
        <asp:Label ID="lblShare" runat="server" Text='<%#Eval("Share") %>'></asp:Label>
    </ItemTemplate>
0
RajeshKdev On

You need to return something from Share method, If you want to assign the value from share() to Label.

Your method returns - void - nothing.

public string share() // Provide your return type.
{
    float share = 0;
    ...
    ...

    return share.ToString(); // return share value after the calculation
}