Update a list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babai28
    New Member
    • Jul 2008
    • 59

    #46
    Btw, when are you showing the data in the datagridview and when are you showing it in list box?
    I smell some ambiguity in your approach, how are tyou displaying the data?

    Comment

    • sbandalli
      New Member
      • Feb 2009
      • 53

      #47
      Hi Siddharth,

      your code worked fine....Thanks a ton........
      One more question is, once the user selects the item from the combox then it should be transfered to another table called Transact table.
      Can you give me hint on how to acheive this. I know I am asking u a lot,but thats how I am able to learn,otherwise I feel I am lost...

      Now I have a datagridview which has columns
      1.categoryname( combobox column) which gets data from categoryname column of Category table

      2. Amount (textbox)

      3. Description(tex t box).

      The above is acheived by following code:(given by u slighlty changed)

      Code:
      SqlDataAdapter da = new SqlDataAdapter("select * from Category", connection);
                           try
                 {
                     cDS.Clear();
                     da.Fill(cDS, "Category");
                                    
                     DataGridViewComboBoxColumn listCol = new DataGridViewComboBoxColumn();
                     DataGridViewTextBoxColumn text = new DataGridViewTextBoxColumn();
                     DataGridViewTextBoxColumn amount = new DataGridViewTextBoxColumn();
                     
      
                     listCol.HeaderText = "categoryname";
                     listCol.DisplayIndex = 0;
                     text.HeaderText = "description";
                     text.DisplayIndex = 1;
                     amount.HeaderText = "amount";
                     amount.DisplayIndex = 2;
      
      
                     listCol.DataPropertyName = "categoryid";
                     listCol.DataSource = cDS.Tables["Category"];
                     listCol.DisplayMember = "categoryname";
                     listCol.ValueMember = "categoryid";
                     dataGridView1.Columns.Add(listCol);
                     dataGridView1.Columns.Add(text);
                     dataGridView1.Columns.Add(amount);
                     
      
      
      
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
      Design of my Datagridview

      Categoryname(co mbocolulmn) Description Amount
      Electricity(pul led from CombC) House 30.00

      Now I want all these in a Transaction table in my DB

      This is How I am trying:

      Code:
       private void apply_Click(object sender, EventArgs e)
            {
                
                     
                SqlDataAdapter categoryDataAdapter = new SqlDataAdapter(new SqlCommand("SELECT * FROM Transact_table", connection));
      
                categoryDataAdapter.InsertCommand = new SqlCommand("databaseinserttransact", connection);
                SqlCommand cmdInsert = categoryDataAdapter.InsertCommand;
                cmdInsert.CommandType = CommandType.StoredProcedure;
                cmdInsert.Parameters.Add(new SqlParameter("@description", SqlDbType.NVarChar, 50, "description"));
                cmdInsert.Parameters.Add(new SqlParameter("@money", SqlDbType.Money, 50, "money"));
                categoryDataAdapter.FillSchema(cDS, SchemaType.Source);
                DataTable pTable = cDS.Tables["Table"]; //Represents one table in DataSet
      
      
                // Inserting the data from the textbox into dataset
      
                DataRow cOrderRow = cDS.Tables["Transact_table"].NewRow(); 
                cOrderRow["description"] = ??????????( I am struck here), dont know how to get data from amount column and moneycolumn and combocolumn
               cOrderRow["money"] =;
               cOrderRow["combocolumndata"] = 
                cDS.Tables["Transact_table"].Rows.Add(cOrderRow);
      
                
      
      
                // Updating the Dataset
                categoryDataAdapter.Update(cDS, "Transact_table");*/
      
               
      
      
      
            }

      I tried creating object of datgridview1com bocolumn, datagridview1te xt global
      and tried to get the Textproperty ,but not working.

      I know my Design sounds weird, As this is my 1st object oriented .NET project,I might be going through complex design,but thats what I can think as of now till I get hold of language properly.

      Thanks a lot

      Comment

      • babai28
        New Member
        • Jul 2008
        • 59

        #48
        In object oriented programming (especially when dealing with UI applications) a lot is guided by the events.
        Events are raised whenever user responds to the UI. For examle a button click is an event, item double click of a listbox was an event which we handled. .Net has ready made events for controls which you can handle, considering the exendibility that microsoft always provide, you can also create your events and handle them.
        You need to be very precise and discrete while deciding which event to handle.
        I guess you are handling the ItemIndexChange d event of the dataGridView Combo box column. I see you have handled the event in the following method
        Code:
        private void apply_Click(object sender, EventArgs e) 
        { 
        
        }
        This means, whenever the itemIndex changed event is raised this method will be called. Right?
        Every event handler method (like the above one) carries two parameters "object sender" . This tells you who is the sender of this event or who is responsible for this event being raised? In case of listBox doubleclick event it was the listBox.Since you never know who a sender can be you have to keep its type the most generic i.e. an object.
        The second parameter is the EventArgs e. This parameter contains every required information about the event.
        If inside your method you type e and then . Visual Studio, by virtue of intellisence, will display a list of all the information that the parameter "e" can display. The solution to your problem lies in that list.
        Let me know if this reply helped you.

        Just a suggestion:
        ComboBox selectedItemInd ex changed event is an extremely wrong event to save data in. Just think it yourself, if you select a wrong item, you will end up saving the same in the database, of course you can change that, but its not worth hitting the database as it increases the cost of application, leave apart the misery that it will bring upon the user with its slowed performance.
        Give a proper thought and then design the application.

        All the best.
        Regards,
        Siddhartha

        Comment

        • sbandalli
          New Member
          • Feb 2009
          • 53

          #49
          Hello Siddharth,
          Thanks for the reply....It is very helpful, I understood about the events very nicely..thanks a lot..
          private void apply_Click(obj ect sender, EventArgs e)
          {

          }

          The above method is just a button click event(The text of the button is "apply"), I should have mentioned in my last question(sorry about that)

          When i click that "apply" button I should store the gridview1 columns contents in my transaction table using storedprocedure . Since my gridview has 3 column

          1.categoryname( combobox)
          2.Amount(textbo x)
          3.Description(t extbox)

          categoryname selected by the user by the pull down list of the combobox, Amount and the description the user enters.

          All these I have to able to store to a database table called "Transactio n" using storedprocedure or any other method is also fine.

          Give me some idea.....How do I proceed with this.

          Thanks a lot for your help Siddharth..
          take care

          Comment

          • babai28
            New Member
            • Jul 2008
            • 59

            #50
            Hi,

            If it is like you get the combobox items (of the ComboBoxColumn) from the category table and the other contents of the gridView from the "Transactio n" table, you may bind the dataGridView to the "Transactio n" table and just use the "DataAdapter.Up date()" method.
            It will be simple to use with minimal code.
            However note that you need to have a primary key in the table on which you wish to use the Update method of the Data Adapter. I don't think you have a primary key in the transation table. So define a column like Transaction_ID.
            Also use the CategoryId column of the Category table in the transaction table (rather than the categoryName column) to be as the foreign key in the table transaction.

            All the best.

            Regards,
            Siddhartha

            Comment

            • sbandalli
              New Member
              • Feb 2009
              • 53

              #51
              Siddharth I created a transaction table with the following columns:

              1. Transactionid (PK)
              2. Categoryid(FK) relating to Category table
              3.Description
              4.Transactionda te
              5.Amount

              Now I have a datagridview, which has a comboboxcolumn, Amount,Descript ion.
              Once the user selects a particular item from the comboboxdropdow n, and enters the respective amount and description, I want to Store all these in Transaction table. Finally my transaction table has to be populated with the user entered data in the Datagridview( Data from Datagridview has to stored in Transaction table)

              SInce transaction table also has category id, How to do this using join???
              Thanks a lot

              Comment

              • babai28
                New Member
                • Jul 2008
                • 59

                #52
                No join required, joins are ususally used to select data from tables, rather put a button called "Save", on click of which you may use the Update Method of the DataAdapter.
                But before that define the Update Statement of the Adapter.

                All the best.
                Regards,
                Siddhartha

                Comment

                • sbandalli
                  New Member
                  • Feb 2009
                  • 53

                  #53
                  I need to put categoryid also in my transaction table which is a foreign key,
                  along with the comboboxcolumn entry, Amount,Descript ion, So How do I category id in my Transaction table which relates to the combobox selected item.

                  suppose in my Category table i have the following

                  categoryid categoryname datecreated
                  1 Grocery 02/19
                  2 electricity 02/20
                  3 cable 02/21

                  Now my combobox pull downlist in my gridview has grocery,Electri city,cable

                  suppose user selects cable from comboboxpulldow n in datagridview and writes Amount column: 20.00 in DGV and Description cablebill in DGV

                  So now transaction table should get all these value in DB + categoryid

                  3 cable 20.00 cablebill

                  So how do I get categoryid in Transaction table???



                  I think 1st step I would try to put combobox,amount and cablebill right??


                  Thanks

                  Comment

                  • babai28
                    New Member
                    • Jul 2008
                    • 59

                    #54
                    Hi,

                    When I gave you the code to display the data in a dataGridView ComboBox I wrote the following piece of code (You may go back and check the code in details) :

                    Code:
                    listCol.DataPropertyName = "CategoryId"; 
                                    listCol.DataSource = ds.Tables["Category"]; 
                                    listCol.DisplayMember = "CategoryName"; 
                                    listCol.ValueMember = "CategoryId"; 
                                    dataGridView1.Columns.Add(listCol);
                    If you see the listCol.ValueMe mber is the CategoryId.
                    While constructing the UpdateQuery of your Adapter make sure you pass the valueMember and not the diplayMember.
                    If you want to know about how to construct the query, you have to search in the net a little bit.

                    Regards,
                    Siddhartha

                    Comment

                    • sbandalli
                      New Member
                      • Feb 2009
                      • 53

                      #55
                      Hello Siddharth,

                      I am really not getting How to do. The place where I am struck is here:

                      I have a table called Transaction table

                      columns are:

                      transaction id (primary key)
                      category id(foreign key )
                      transactiodate
                      description
                      amount

                      Now I have a datagridview
                      Columns of datagridview are:
                      comboboxcolumn( the pull downlist from which the user slects particular category)
                      Amount
                      description

                      Now when the user selects an item from the comboboxcolumn of the datagridview (suppose user selects electricity) and enters in the description column as "energy" and amount as "$20". Now I have to transfer above mentioned data to my transaction table plus I have to generate the respective category id of electricity from the Category table to transaction table.

                      I need to put all the data in the Transaction table when the user clicks apply button near the Datagrdivew .

                      I hope I am not confusing you.
                      I need help on this.

                      Thanks Siddhartha

                      Comment

                      • sbandalli
                        New Member
                        • Feb 2009
                        • 53

                        #56
                        Do I have to put the data from the datagridview to the Datatable of the Dataset and then try to put it into the transaction table of the database. If so , here is the code i am using to put it in data table

                        DataTable pTable = cDS.Tables["Table"];
                        DataRow cOrderRow = cDS.Tables["Transact_table "].NewRow();
                        cOrderRow["amount"] = ?????? // how do I select the text from the texbot of the datagridview;
                        cOrderRow["descriptio n"] = ?????? // how do I select the text from the textbox of the datagridview;

                        And Also i need to get the category id from the category table corresponding to the user selected combobox value of the datagridview and store it in transact table which is a foreign key of the transact table.

                        My category table has categoryid, categoryname,da tecreated.

                        Comment

                        • sbandalli
                          New Member
                          • Feb 2009
                          • 53

                          #57
                          Code:
                           void savetotransact_table()
                                  {
                                      
                                    DataTable pTable1 = cDS.Tables["Table"];
                                    DataRow cOrderRow = cDS.Tables["Transact_table"].NewRow();
                                    cOrderRow["amout"] = dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                          
                                   cOrderRow["description"] = dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                                   cDS.Tables["Transact_table"].Rows.Add(cOrderRow);
                          
                          
                                   SqlDataAdapter categoryDataAdapter = new SqlDataAdapter(new SqlCommand("SELECT * FROM Transact_table", connection));
                          
                                   categoryDataAdapter.InsertCommand = new SqlCommand("databaseinserttransact_table", connection);
                                   SqlCommand cmdInsert = categoryDataAdapter.InsertCommand;
                                   cmdInsert.CommandType = CommandType.StoredProcedure;
                                   cmdInsert.Parameters.Add(new SqlParameter("@amount", SqlDbType.Money, 50, "amount"));
                                   cmdInsert.Parameters.Add(new SqlParameter("@description", SqlDbType.VarChar, 50, "description"));
                                   categoryDataAdapter.FillSchema(cDS, SchemaType.Source);//fills method of dataadapter loads dataset with data from database
                          
                                   // Updating the Dataset
                                   categoryDataAdapter.Update(cDS, "Transact_table");
                          
                                  }
                          I am also trying the above code,not working giving me error at the 2nd line itself
                          DataRow cOrderRow = cDS.Tables["Transact_table "].NewRow();
                          I dont know whether I am trying it in a rightway or not???

                          Comment

                          • sbandalli
                            New Member
                            • Feb 2009
                            • 53

                            #58
                            Also I wanted to just tell u that, my datagridview is not bounded to any datasource, only comboboxcolumn of datagridview is bounded to Categorytable

                            listCol.DataSou rce = cDS.Tables["Category"];

                            Thanks..

                            Comment

                            • sbandalli
                              New Member
                              • Feb 2009
                              • 53

                              #59
                              Siddharth did u get my last 2 questions, because when I see the thread I could not see my questions, So I was not sure whether u got my previous questions.After # 50 I dont see any more questions or answers.

                              Comment

                              • sbandalli
                                New Member
                                • Feb 2009
                                • 53

                                #60
                                hello Siddhartha,
                                I am not able to contact you or post any questions, I dont know whether you were able to see my previous couple of questions.
                                Please do let me know if you are able to see my questions
                                Thanks

                                Comment

                                Working...