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. Windows Forms
  4. problem with datatable [modified]

problem with datatable [modified]

Scheduled Pinned Locked Moved Windows Forms
csharpcsshelpquestion
5 Posts 3 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.
  • S Offline
    S Offline
    Sider89
    wrote on last edited by
    #1

    DataTable dt;

    private void Permissions_Load(object sender, EventArgs e){
                      dt = new DataTable();
                      dt.Columns.Add("Feature", typeof(string));
                      dt.Columns.Add("None", typeof(bool));
                      dt.Columns.Add("ReadOnly", typeof(bool));
                      dt.Columns.Add("ReadWrite", typeof(bool));

    datagridview1.Datasource=dt;
    }

    private void btnSave_Click(object sender, EventArgs e){

    in the grid when i'll click on the any checkbox i want to pass parameter to this function SetPermission( , , );
    parameter will be from the two enumeratons below
    How manage it?

    here picture Picture
    }

    public void SetPermission(Permission feature, PermissionType permission, int userID) {
                      SqlCommand cmd = new SqlCommand("Insert into Permissions      (UserID,Feature,Permission)Values(@UserID,@Feature,@Permission)", con);

    cmd.Parameters.Add(new SqlParameter("UserID", userID));

    }

    public enum Permission : int
          {
                Country = 0,
                Store = 1,
                City = 2
          }

    public enum PermissionType : byte
          {
                None = 0,
                ReadOnly = 1,
                ReadWrite = 2
          }

    C# Developer

    modified on Monday, October 26, 2009 3:18 PM

    H A 2 Replies Last reply
    0
    • S Sider89

      DataTable dt;

      private void Permissions_Load(object sender, EventArgs e){
                        dt = new DataTable();
                        dt.Columns.Add("Feature", typeof(string));
                        dt.Columns.Add("None", typeof(bool));
                        dt.Columns.Add("ReadOnly", typeof(bool));
                        dt.Columns.Add("ReadWrite", typeof(bool));

      datagridview1.Datasource=dt;
      }

      private void btnSave_Click(object sender, EventArgs e){

      in the grid when i'll click on the any checkbox i want to pass parameter to this function SetPermission( , , );
      parameter will be from the two enumeratons below
      How manage it?

      here picture Picture
      }

      public void SetPermission(Permission feature, PermissionType permission, int userID) {
                        SqlCommand cmd = new SqlCommand("Insert into Permissions      (UserID,Feature,Permission)Values(@UserID,@Feature,@Permission)", con);

      cmd.Parameters.Add(new SqlParameter("UserID", userID));

      }

      public enum Permission : int
            {
                  Country = 0,
                  Store = 1,
                  City = 2
            }

      public enum PermissionType : byte
            {
                  None = 0,
                  ReadOnly = 1,
                  ReadWrite = 2
            }

      C# Developer

      modified on Monday, October 26, 2009 3:18 PM

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      From reading your question, which was slightly difficult because your actual question is mixed up in the middle of your code, I would suggest that you redesign your DataTable

      private void Permissions\_Load(object sender, EventArgs e){
          dt = new DataTable();
          dt.Columns.Add("Feature", typeof(string));
          dt.Columns.Add("None", typeof(bool));      //<======================== Here ===============
          dt.Columns.Add("ReadOnly", typeof(bool));      //<======================== Here ===============
          dt.Columns.Add("ReadWrite", typeof(bool));      //<======================== Here ===============
      
          datagridview1.Datasource=dt;
      }
      

      The three columns marked //<======================== Here =============== are the three possible values of PermissionType and should be one column, probably, without thinking about it too much, of type int. This would mean that the design of your Form and Grid would need to change which would therefore probably change the question. Also you refer in the question bit to the any checkbox without having previously mentioned it, let alone describe its purpose.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      S 1 Reply Last reply
      0
      • H Henry Minute

        From reading your question, which was slightly difficult because your actual question is mixed up in the middle of your code, I would suggest that you redesign your DataTable

        private void Permissions\_Load(object sender, EventArgs e){
            dt = new DataTable();
            dt.Columns.Add("Feature", typeof(string));
            dt.Columns.Add("None", typeof(bool));      //<======================== Here ===============
            dt.Columns.Add("ReadOnly", typeof(bool));      //<======================== Here ===============
            dt.Columns.Add("ReadWrite", typeof(bool));      //<======================== Here ===============
        
            datagridview1.Datasource=dt;
        }
        

        The three columns marked //<======================== Here =============== are the three possible values of PermissionType and should be one column, probably, without thinking about it too much, of type int. This would mean that the design of your Form and Grid would need to change which would therefore probably change the question. Also you refer in the question bit to the any checkbox without having previously mentioned it, let alone describe its purpose.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        S Offline
        S Offline
        Sider89
        wrote on last edited by
        #3

        Henry Minute I'll try to explain it Here is link of Picture http://img9.imageshack.us/i/35025568.jpg/ When I click on any checkbox here,I want to pass parameter in set function, for example like this: if I'll click on row Stores, Column read only's expedient checkbox item i want to pass SetPermission(1,1, ...) because in enumeration stores id is 1 and read only's id is 1 too.

        C# Developer

        H 1 Reply Last reply
        0
        • S Sider89

          Henry Minute I'll try to explain it Here is link of Picture http://img9.imageshack.us/i/35025568.jpg/ When I click on any checkbox here,I want to pass parameter in set function, for example like this: if I'll click on row Stores, Column read only's expedient checkbox item i want to pass SetPermission(1,1, ...) because in enumeration stores id is 1 and read only's id is 1 too.

          C# Developer

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          I am not able to view your image, as I am having bandwidth problems and it would take about 20mins to load (I probably wouldn't anyway - no reflection on yourself but I am very distrustful of following links. :) ) My basic problem with the code you presented originally is that nowhere in it do you load any existing data. I can only assume therefore that the part you are working on is for new data only. On that basis if you must use a grid it would make things a whole lot easier to code for if, instead of 3 (or however many options there are) CheckBoxColumns, you had 1 ComboBoxColumn with its items set to the possible options. What would happen if the number of options suddenly increased to 20+, are you going to add that number of additional CheckBoxColumns? In any event, as soon as the user makes a selection you are saving the data, so why use a grid? It would be far, far easier to simply have appropriate controls on a Form or Panel and clear them on a successful save ready for the next entry and leave them populated on any error so that corrections can be made. Sorry not to be more help, but it really seems to me that your design needs some more thought. :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          1 Reply Last reply
          0
          • S Sider89

            DataTable dt;

            private void Permissions_Load(object sender, EventArgs e){
                              dt = new DataTable();
                              dt.Columns.Add("Feature", typeof(string));
                              dt.Columns.Add("None", typeof(bool));
                              dt.Columns.Add("ReadOnly", typeof(bool));
                              dt.Columns.Add("ReadWrite", typeof(bool));

            datagridview1.Datasource=dt;
            }

            private void btnSave_Click(object sender, EventArgs e){

            in the grid when i'll click on the any checkbox i want to pass parameter to this function SetPermission( , , );
            parameter will be from the two enumeratons below
            How manage it?

            here picture Picture
            }

            public void SetPermission(Permission feature, PermissionType permission, int userID) {
                              SqlCommand cmd = new SqlCommand("Insert into Permissions      (UserID,Feature,Permission)Values(@UserID,@Feature,@Permission)", con);

            cmd.Parameters.Add(new SqlParameter("UserID", userID));

            }

            public enum Permission : int
                  {
                        Country = 0,
                        Store = 1,
                        City = 2
                  }

            public enum PermissionType : byte
                  {
                        None = 0,
                        ReadOnly = 1,
                        ReadWrite = 2
                  }

            C# Developer

            modified on Monday, October 26, 2009 3:18 PM

            A Offline
            A Offline
            AhmedMasum
            wrote on last edited by
            #5

            Try the following. It may help you. Remember that you have to initialize check box values at first.

            private void Form1_Load(object sender, EventArgs e)
                    {         
                        int p = dataGridView1.Rows.Add();
                        dataGridView1.Rows[p].Cells[0].Value = true.ToString();
                       dataGridView1.Rows[p].Cells[1].Value = false.ToString();
                     
            
                    }
            

            Handle the following event of datagridview

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
                    {
                                   
                        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals(false.ToString()))
                        {
                            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true.ToString();
            
                            MessageBox.Show("true");
                        }
                        else
                        {
            
                            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false.ToString();
            
                            MessageBox.Show("false");
                        }
                            
                    }
            

            call the method in appropriate if else condition.

            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