Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. listview.

listview.

Scheduled Pinned Locked Moved C#
visual-studioquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    angels777
    wrote on last edited by
    #1

    hi.. why when i drag out a listview in vs 2005. i cant get teh function of listview.selectedindex ? only selecteditem .. i want to get the id of the data selected ..

    D 1 Reply Last reply
    0
    • A angels777

      hi.. why when i drag out a listview in vs 2005. i cant get teh function of listview.selectedindex ? only selecteditem .. i want to get the id of the data selected ..

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      You have SelectedItems and SelectedIndices - check the Count property of either one of these to make sure there is at least one item selected and if you're just wanting the first item selected then something like this:

      int firstSelectedIndex1 = listView1.SelectedItems[0].Index;
      int firstSelectedIndex2 = listView1.SelectedIndices[0];

      Dave

      A 1 Reply Last reply
      0
      • D DaveyM69

        You have SelectedItems and SelectedIndices - check the Count property of either one of these to make sure there is at least one item selected and if you're just wanting the first item selected then something like this:

        int firstSelectedIndex1 = listView1.SelectedItems[0].Index;
        int firstSelectedIndex2 = listView1.SelectedIndices[0];

        Dave

        A Offline
        A Offline
        angels777
        wrote on last edited by
        #3

        hi.. sorry.. i think i would like to use up the DataGridView called dvCustomer under private void dvCustomer_SelectionChanged(object sender, EventArgs e) { } when i click on the datagridview row... i would like to get the user ID from database. so that i can do the update as below ..

        private void cust_update_Click(object sender, EventArgs e)
        {
        bool success;
        success = dataAccess.AddCustomer(Datagridview ID, cust_name.Text, cust_add.Text, cust_account.Text, cust_phone.Text...);
        if (success)
        {
        MessageBox.Show("Data updated!");
        }
        else
        {
        MessageBox.Show("Update FAIL!!!");
        }
        }

        in my dataAccess class

        public bool AddCustomer(string function, string custName, string custAdd, string custAcc, string custPhone.....)
        {
        bool success = false;
        string query = string.Empty;
        try
        {
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.Add(new SqlParameter("@companyName", custName));
        cmd.Parameters.Add(new SqlParameter("@customerAdd", custAdd));
        cmd.Parameters.Add(new SqlParameter("@attName", custAdd));
        cmd.Parameters.Add(new SqlParameter("@customerAcc", custAcc));
        cmd.Parameters.Add(new SqlParameter("@customerPhone", custPhone));

        query += "UPDATE CUSTOMER set company_name= @companyName, company_add= @customerAdd, att_name= @attName, account_num= @customerAcc, phone= @customerPhone...........";
        query += " where cust_id = ?????????? ";

                    cmd.CommandText = query;
        
                    if (cn.State == ConnectionState.Closed) cn.Open();
                    cmd.Connection = cn;
        
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        success = true;
                        return success;
                    }
        
        A 1 Reply Last reply
        0
        • A angels777

          hi.. sorry.. i think i would like to use up the DataGridView called dvCustomer under private void dvCustomer_SelectionChanged(object sender, EventArgs e) { } when i click on the datagridview row... i would like to get the user ID from database. so that i can do the update as below ..

          private void cust_update_Click(object sender, EventArgs e)
          {
          bool success;
          success = dataAccess.AddCustomer(Datagridview ID, cust_name.Text, cust_add.Text, cust_account.Text, cust_phone.Text...);
          if (success)
          {
          MessageBox.Show("Data updated!");
          }
          else
          {
          MessageBox.Show("Update FAIL!!!");
          }
          }

          in my dataAccess class

          public bool AddCustomer(string function, string custName, string custAdd, string custAcc, string custPhone.....)
          {
          bool success = false;
          string query = string.Empty;
          try
          {
          SqlCommand cmd = new SqlCommand();
          cmd.Parameters.Add(new SqlParameter("@companyName", custName));
          cmd.Parameters.Add(new SqlParameter("@customerAdd", custAdd));
          cmd.Parameters.Add(new SqlParameter("@attName", custAdd));
          cmd.Parameters.Add(new SqlParameter("@customerAcc", custAcc));
          cmd.Parameters.Add(new SqlParameter("@customerPhone", custPhone));

          query += "UPDATE CUSTOMER set company_name= @companyName, company_add= @customerAdd, att_name= @attName, account_num= @customerAcc, phone= @customerPhone...........";
          query += " where cust_id = ?????????? ";

                      cmd.CommandText = query;
          
                      if (cn.State == ConnectionState.Closed) cn.Open();
                      cmd.Connection = cn;
          
                      if (cmd.ExecuteNonQuery() > 0)
                      {
                          success = true;
                          return success;
                      }
          
          A Offline
          A Offline
          angels777
          wrote on last edited by
          #4

          may i know how do i get the ID from the datagridview ?

          D 1 Reply Last reply
          0
          • A angels777

            may i know how do i get the ID from the datagridview ?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            You may be better off reposting in a new thread as this is no longer about the listView as suggested by the subject! I never use the DataGridView myself so can't be of much help.

            Dave

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups