c# - 如何从 datagridview 单元格中检索文本并将其显示在按钮中?

标签 c# winforms text button multiline

如何从 DataGridView 单元格中检索文本并将其显示在按钮上。 基本上我的项目有两种形式(Form1 和 Form2)。

-在 Form1 中,我有两个按钮(Starter 和 Main)。这两个按钮都有点击事件,它们调用数据库 sql 查询并生成按钮形式的记录。

-在 Form2 我有一个按钮(Starter)。此外,单击事件上的此按钮会调用数据库 sql 查询并在 DatagridView 中生成记录。

现在在 Form2 中,当我在 Quantity In Stock 列下的单元格内双击时,会弹出一个对话框,允许我在那个特定的位置输入数字细胞。 假设第 1 行:

Soup     Starter     10     <Allways On Stock>

因此,基于此,我如何获取 cell = 10 的值并将其显示在按钮的右下角(在本例中为按钮 Soup) 像这样:

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

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

提前致谢...

亲切的问候

拉佩奇


这里是datagridview的cellclick事件的代码

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // make sure that the click was in the right column
            if (e.ColumnIndex == 2)  // I used 1 here because I didn't put a column for FoodType, you should use 2.
            {
                // Give it a value 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 对话框...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DtposApplication
{
    public partial class InputBox : Form
    {
        public InputBox(string text, string caption, string defaultValue)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            this.Text = caption;  //.Clone().ToString();


            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 上创建按钮,然后获取数据库记录并为这些按钮赋值


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

最佳答案

您可以在按钮顶部的确切位置放置一个标签 然后更新标签

关于c# - 如何从 datagridview 单元格中检索文本并将其显示在按钮中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4653378/

相关文章:

c# - OpenFileDialog 正在最小化父表单

c# - Combobox.Text.Equals 不测试预期值

c# - 从 C# 调用 C++ 函数并将数据发送回 C#

c# - 在 C# 中,如何在不逐行搜索的情况下在大型文本文件中搜索字符串?

c# - Asp.net 控件在回发后不更新

c# - 使用 C# 代码运行 "Update-Database"Power Shell 命令时出现问题

c# - 将菜单选项插入 ApplicationIcon 菜单

c# - 设置文件创建日期的最佳方法,通过文件流下载

ios - 调整文本大小以适应 Swift 中的标签

r - 使用 R 从 Pubmed 数据中的隶属关系中提取大学名称