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

发布于 2024-10-11 05:12:26 字数 7107 浏览 7 评论 0原文

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

-在 Form1 中,我有两个按钮(Starter 和 Main)。这两个按钮在单击事件时都会调用数据库 sql 查询并将记录生成为按钮。

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

现在,在Form2中,当我双击库存数量列下的单元格时,会弹出一个对话框,允许我在该特定单元格中输入数字。 让我们说 Row-1:

Soup     Starter     10     <Allways On Stock>

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

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

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

提前致谢...

亲切的问候

lapeci


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

How can I retrieve text from DataGridView Cell and displayit on the button.
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.

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:

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

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

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

Thanks in advance...

Kind regards

lapeci


here is the code of cellclick event of datagridview

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

And this is the InputBox dialog to enter quantity in to the cell...

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

is the code how im creating 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 = "";
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-10-18 05:12:26

您可以将标签准确地放置在按钮顶部您想要的位置
然后更新标签

You could put a label on top of the button exactly where you want it
and then update the label

十年不长 2024-10-18 05:12:26

看看这个:

多行文本作为按钮标签 - Windows 窗体

当您获得值 10 时,尝试使用该值设置按钮文本属性。您应该进行一些格式化(相应地添加空格)以使文本适合按钮右下角的部分。

更详细的解释:

当弹出对话框时,您输入值 10。当您在数据网格单元格中设置此数字时(我认为您有某种事件处理程序来做这项工作),您还应该设置按钮的文本属性 btnSoup.Text = 10 在同一事件处理程序中。要对齐按钮中的文本,请使用我上面提供的链接。

Check this out:

Multiline Text as the button label - Windows Forms

At the time you get the value 10, try to set the button text property with this value. You should do some formatting (adding spaces accordingly) to fit the text in the bottom-right part of the button.

More detailed explanation:

When the dialog box pops up you enter the value 10. When you set this number in the datagrid cell (I think you have some kind of event handler doing this job) you should also set the button's text property btnSoup.Text = 10 within the same event handler. To align the text in the button, use the link I provided you above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文