如何从dgView单元格检索文本并在按钮中显示它? [英] How to retrieve text from dgView cell and displayit in the button?

查看:63
本文介绍了如何从dgView单元格检索文本并在按钮中显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

如何从Form2的datagridView单元中检索文本并在Form1的按钮中显示它?

基本上,我在项目中有两种形式(Form1和Form2).
-在 Form1 中,我有两个按钮(启动器"和主要").这两个按钮都在单击事件中发生调用,它们调用数据库sql-query并将其生成为按钮形式的记录.
-在 Form2 中,我有一个按钮(启动器).另外,单击事件上的此按钮调用数据库sql-query并在DatagridView中生成记录. DataGridView有四列:

现在在 Form2 中,当我在库存数量"列下的单元格内双击时,会弹出一个对话框,允许我在该特定单元格中输入数字.可以说第1行:像这样...

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



因此,基于此,我该如何获取该单元格的值= 10并在Button(在本例中为Button Soup)的右下角显示.这就是汤按钮的外观(在Form1中):



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



这是datagridview1_cellclick事件的代码(在Form2中)...



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

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;
         }
      }
   }
}



这是InputBox对话框窗体的代码,用于在DataGridView的单元格中输入数量...



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();
  }
}



这是代码,如何在Form1上创建按钮,然后将数据库记录和值赋给这些按钮...



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 = "";
  }
}



有人可以帮我解决这个问题....

提前谢谢...

亲切的问候

lapeci



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

Thanks in advance...

Kind regards

lapeci

推荐答案

要问如何使按钮外观看起来有些笨拙.

如果它是WPF应用程序,这会更容易,但是由于您使用的是Windows Forms ...

不能通过设置按钮Text属性来做到这一点.使用股票按钮根本无法进行复合格式.

我建议您制作自己的Button控件,并公开第二个Text属性Text2.然后,您将创建一个按钮本身大小的位图,并在第一行上绘制Text,然后在下一行上绘制Text2(右对齐).您必须进行数学运算才能弄清楚文本应该去哪里,但这并不难.获得位图后,将其设置为BackgroundIimage按钮.
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.


这篇关于如何从dgView单元格检索文本并在按钮中显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆