Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone

How can I retrieve text from datagridView cell (of Form2) and displayit in the button (of Form1)?

Basically I have two forms on my project (Form1 & Form2).
-In Form1 I have two buttons (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 (Starter). Also this button on click event calls database sql-query and generates records in DatagridView. DataGridView has four columns:

Now in Form2 when I double_click inside the cell under the Quantity 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   Quantity In Stock   Status.
------------------------------------------------------------------
Soup       Starter    10                  Allways On Stock


So 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):

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


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

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   if (e.ColumnIndex == 2)
   {
      // Value is given here 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 Form to enter quantity in to the Cell of DataGridView...

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...

C#
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
Posted
Comments
dan!sh 11-Jan-11 15:19pm    
Does form1 opens the form2? And when should the button get created?
LAPEC 11-Jan-11 19:37pm    
Hi D@nish
Yes Form1 does open Form2,
The Button can be created at any time (on Form1) whenever the button click_event is fired on Form1...

kind regards

lapeci

1 solution

That's a lot of fluff to ask how you can make a button look a certain way.

This would be easier if it was a WPF app, but since you're using Windows Forms...

There's no way to do it with setting the button Text property. Compound formatting is simply not possible using the stock button.

I'd recommend making your own Button control, exposing a second Text property, Text2. You'd then create a bitmap the size of the button itself and paint Text onto the first line, then paint the Text2 on the next line, right justified. You'll have to do the math to figure out where the text is supposed to go, but that's not very hard. Once you have the bitmap, set it as the button BackgroundIimage.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900