6 Replies - 12452 Views - Last Post: 12 January 2011 - 05:50 PM Rate Topic: -----

#1 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:19 PM

Dear People

I have two forms on my project (Form1 & Form2).

-In Form1 I have two Buttons (called Starter & Main). Both these buttons on click event, they call database sql-query and genereate into form the records as Buttons.
-In Form2 I have a Button (called Starter) and DataGridView. Also this button on click event calls database sql-query and generates records in DatagridView. DataGridView has four columns:

(Note: On columns [0] and [1] data are genereated from database, the columns [2] and [3] are template columns genereated at run-time also filled with data at run-time)

Now in Form2 when I double_click inside the Cell under the Qty In Stock column, a dialog-box pops up and allows me to enter the number in to that particular cell. Lets say Row-1: like so...

FoodName     FoodType     Qty In Stock     Status
-----------------------------------------------------------
Soup         Starter      10               Allways On Stock


Based on this: How can I take the value of that cell = 10 and dispalyit on the bottom-right corner of the Button (in this case button Soup). This is what the Soup Button should look like (in Form1): (Note: The Button called Soup is generated from database...)

##############
#   Soup     #
#         10 #
##############


Here is the code of dataGridView1_CellClick event (in Form2)...

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                // Value is given in case the cell is empty
                string cellContent = "0";
                if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
                {
                    cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
                }
                
                using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
                {
                    if (ib.ShowDialog() == DialogResult.OK)
                    {
                        this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
                        cellContent = ib.Result;
                    }
                }
            }
        }


This is the code of InputBox dialog where it allows me to enter quantity number in to the Cell of dataGridView1...

public partial class InputBox : Form
    {
        public InputBox(string text, string caption, string defaultValue)
        {
            InitializeComponent();

            this.Text = caption;

            Size size;
            using (Graphics g = this.CreateGraphics())
            {
                Rectangle screen = Screen.PrimaryScreen.WorkingArea;
                SizeF sizeF = g.MeasureString(text, lblPrompt.Font, screen.Width - 20);
                size = sizeF.ToSize();
                size.Width += 4;
            }

            if (size.Width < 310)
            {
                size.Width = 310;
            }
            Size clientSize = this.ClientSize;
            clientSize.Width += size.Width - lblPrompt.Width;
            clientSize.Height += size.Height - lblPrompt.Height;
            this.ClientSize = clientSize;
            lblPrompt.Text = text;
            txtResult.Text = defaultValue;
            this.DialogResult = DialogResult.Cancel;
        }

        void CancelButtonclick(object sender, System.EventArgs e)
        {
            result = null;
            this.Close();
        }

        void AcceptButtonclick(object sender, System.EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            result = txtResult.Text;
            this.Close();
        }

        string result;

        public string Result
        {
            get
            {
                return result;
            }
        }

        private void btnSeven_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnSeven.Text + "7";
        }

        private void btnTwo_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnTwo.Text + "2";
        }

        private void btnOne_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnOne.Text + "1";
        }

        private void btnSix_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnSix.Text + "6";
        }

        private void btnFive_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnFive.Text + "5";
        }

        private void btnFour_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnFour.Text + "4";
        }

        private void btnNine_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnNine.Text + "9";
        }

        private void btnEight_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnEight.Text + "8";
        }

        private void btnThree_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnThree.Text + "3";
        }

        private void btnZero_Click(object sender, EventArgs e)
        {
            txtResult.Text += btnZero.Text + "0";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Clear();
            txtResult.Focus();
        }
    }


This is the code how to create Buttons on Form1, and then take the database records and asign values to these buttons...

private void FoodAddButtons(DataTable table)
        {
            int xpos = 5;
            int ypos = 5;
            int space = 2;
            VistaButtonTest.VistaButton newButton = null;
            DtposMenuBS.Sort = "FoodPrice";
            try
            {
                foreach (DataRowView dr in DtposMenuBS.List)
                {
                    newButton = new VistaButtonTest.VistaButton();
                    newButton.ButtonText = dr["FoodName"].ToString();
                    newButton.AutoEllipsis = true;
                    newButton.Width = 152;
                    newButton.Height = 70;
                    newButton.CornerRadius = 4;
                    newButton.Font = new System.Drawing.Font("Arial Narrow", 15.00F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    newButton.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
                    newButton.ForeColor = System.Drawing.Color.Black;
                    newButton.HighlightColor = System.Drawing.Color.DarkGray;
                    newButton.GlowColor = System.Drawing.Color.DimGray;
                    if (xpos + newButton.Width > this.FoodMenuPanel.ClientSize.Width)
                    {
                        ypos += newButton.Height + space;
                        xpos = 5;
                    }
                    newButton.Location = new Point(xpos, ypos);
                    xpos += newButton.Width + space;
                    newButton.Click += ItemSelection1;
                    this.FoodMenuPanel.Controls.Add(newButton);
                }
            }
            finally
            {
                DtposMenuBS.Sort = "";
            }
        }


Could someone help me please and solve this problem....

Thanks in advance...

Kind regards

lapeci

Is This A Good Question/Topic? 0
  • +

Replies To: C#: How to retrieve cell value of DataGridView and displayit in the bu

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:22 PM

If I'm reading your question correctly, you might want to consider creating a userControl. You can have it behave like a button and allow you to set two things: ItemName and Stock.
Was This Post Helpful? 0
  • +
  • -

#3 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:26 PM

Hi Sergio

Im confused can you be more specific please

lapeci
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:33 PM

Create one of these:

Posted Image

http://www.google.co...ontrol+tutorial


Basically, after you've done all the grunt work, you'll have a nice control you can drag onto the design view. Ideally you'll have a ProductName and Stock property on that control that will allow you to set the information you want.

You want to create a generic control to use for all products.

This post has been edited by Sergio Tapia: 12 January 2011 - 03:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:44 PM

Hi Sergio

I may sound a bit rediculus and embarrising but to be honest with you, im new to C# programming, I mean I can create a user control limke you said, but then I'm gona probably have to modify my code aswell and I'm gona be stuck....
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 03:47 PM

Trust me dude, after you've finished this little control you'll be much more confident in your skills AND you'll learn a ton.

Please follow a tutorial and see what it actually is before giving up. You might even catch on to it very quickly!
Was This Post Helpful? 0
  • +
  • -

#7 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Re: C#: How to retrieve cell value of DataGridView and displayit in the bu

Posted 12 January 2011 - 05:50 PM

Hi Sergio

If its not to much trouble for you, maybe you can have a look at the code and see if you can modify it please...
It would mean the world to me honestly...


Thanks in advance

Kind regards

lapeci
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1