locked
Trying to fine the value of a column in a listview RRS feed

  • Question

  • User-1999980185 posted

    I need to find all value in a column in a listview.   The below code will do this in VB.   What would be the code in C#

    For Each Var1 as ListViewItem in Listview1.Items

    Dim Lincome as Label = Var1.FindControl("dincome')

    dim Lincome_Data as string = Lincome.text

    Next

    Monday, August 6, 2018 8:23 AM

All replies

  • User-492460945 posted

    Hi,

    If you just want to convert your VB code to C#, there are many sites which can do this for free. Check below links.

    Anyway, here's your code in C#

    foreach (ListViewItem Var1 in Listview1.Items)
    {
      Label Lincome = Var1.FindControl(@"dincome')
      string Lincome_Data = Lincome.text
    }

    Thanks,

    RajeshV.

    Monday, August 6, 2018 8:40 AM
  • User-1999980185 posted

    Thanks I am still having problem.  I am new in C#.    I think my syntax is killing me.  See code and error message below;

    Code below:
                   foreach (ListViewItem var1 in ListView_Budget.Items)
    
                    {
                    Label Lincome = var1.FindControl(@"dincome")
                    string Lincome_Data = Lincome.Text;
    
                    }
    
    Error Message below:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1002	; expected	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	223	Active
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0266	Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	222	Active
    

    Monday, August 6, 2018 9:08 AM
  • User-492460945 posted

    A semi-colon missing (';'). Check updated one below.

    foreach (ListViewItem var1 in ListView_Budget.Items)
    
                    {
                    Label Lincome = var1.FindControl(@"dincome");
                    string Lincome_Data = Lincome.Text;
    
                    }
    Monday, August 6, 2018 9:11 AM
  • User-1999980185 posted

    Please see new code and new error message:

    Code below:
                   public void Display_Page_Routine()
            {
              
                foreach (ListViewItem var1 in ListView_Budget.Items)
    
                    {
                    Label Lincome = var1.FindControl(@"dincome");
                    string Lincome_Data = Lincome.Text;
    
                    }
    
            }
    
    Error Message below:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0266	Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	222	Active
    

    Monday, August 6, 2018 9:16 AM
  • User-369506445 posted

    hi

    try below

    protected void ListView_Budget_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item is ListViewDataItem)
        {
             var yourLabel = e.Item.FindControl("dincome") as Label;
             // ...
        }
    }

    Monday, August 6, 2018 9:28 AM
  • User-492460945 posted

    Need to convert the control to Label as below.

    Label Lincome = (Label)var1.FindControl(@"dincome");
    Monday, August 6, 2018 9:31 AM
  • User-1999980185 posted

    Hello All,  I wish to call the method Display_Page_Routine to get the values I am seeking.   I do not wish to execute this code in an ItemDataBound event.  What must I do.  I am new in C# and confused.  

    Monday, August 6, 2018 12:20 PM
  • User-369506445 posted

    why <g class="gr_ gr_39 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="39" data-gr-id="39">you don't</g> want to use <g class="gr_ gr_40 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="40" data-gr-id="40">ItemDataBound  </g><g class="gr_ gr_41 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="41" data-gr-id="41"><g class="gr_ gr_40 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="40" data-gr-id="40">event</g> ?</g>

    please try below

                foreach (ListViewItem var1 in ListView_Budget.Items)
                {
                    Label Lincome = var1.FindControl(@"dincome") as Label;
                     string Lincome_Data = Lincome==null ? "" : Lincome.Text;
    
                }

    Monday, August 6, 2018 12:28 PM
  • User-893317190 posted

    Hi db2tech@yahoo.com,

    If you want to find all value in a column in a listview , since ItemDataBound  event triggers every time data in one row is bound , you couldn’t find all your value until  the last binding.
    I suggest you could write your code in  listview’s DataBound event ,the event only fires when all the data is bound to the listview, so you could get all the value of a column in the listview.
    I have made a simple sample. Below is my code.

    <asp:ListView ID="ListView_Budget" runat="server" DataSourceID="SqlDataSource1" ItemPlaceholderID="itemPlaceholder" OnDataBound="ListView1_DataBound">
                     <LayoutTemplate   >
                         <table   class="table table-bordered">
    
                              <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                         </table>
                   
    
                     </LayoutTemplate>
                     <ItemTemplate>
                         <tr>
                            
                              <td><asp:Label ID="dincome" runat="server" Text=' <%# Eval("name") %>'></asp:Label></td>
                               <td><asp:Label ID="Label1" runat="server" Text=' <%# Eval("sid") %>'></asp:Label></td>
                               <td><asp:Label ID="Label2" runat="server" Text=' <%# Eval("city") %>'></asp:Label></td>
                         </tr>
                        
                     </ItemTemplate>
                    
                 </asp:ListView>
                <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
    
                 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebFormCases.Models.supplyModelConnectionString %>" SelectCommand="SELECT * FROM [Suppliers]"></asp:SqlDataSource>
    

    Code behind.

    public void Display_Page_Routine()
            {
              
                foreach (ListViewItem var1 in ListView_Budget.Items)
    
                {
                    Label Lincome = var1.FindControl(@"dincome") as Label;
                    string Lincome_Data = Lincome.Text;
                    Label3.Text += Lincome_Data + " &nbsp;&nbsp;&nbsp;";
    
                }
    
            }
    
            protected void ListView1_DataBound(object sender, EventArgs e)
            {
                Display_Page_Routine();
            }
    

    Since I don’t know what you want to do using  all value in a column , I have shown all value in a label control. If  this couldn’t meet your requirement ,please  post more of your code to let us know your requirement.
    For more information about listview’s onDataBound event , please refer to

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound(v=vs.110).aspx

    Best regards ,

    Ackerly Xu

    Wednesday, August 8, 2018 6:08 AM