Click here to Skip to main content
15,893,644 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC5-Jan-11 13:23
LAPEC5-Jan-11 13:23 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute5-Jan-11 13:24
Henry Minute5-Jan-11 13:24 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 2:32
LAPEC6-Jan-11 2:32 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 4:36
Henry Minute6-Jan-11 4:36 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 6:01
LAPEC6-Jan-11 6:01 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 8:29
Henry Minute6-Jan-11 8:29 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 8:35
LAPEC6-Jan-11 8:35 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 11:04
Henry Minute6-Jan-11 11:04 
Roni,

The first part of what you want is fairly simple.

Write an event handler for the DataGridview.CellClick or DataGridView.CellDoubleClick events. I have done it here for the Click event but after writing it I thought that it would actually be better on the DoubleClick one, quite simply because that way you can be sure the user meant it and didn't accidentally or aimlessly click in the grid. It is up to you. The code will be the same for both events.

This code uses InputBox, code below.
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // make sure that the click was in the right column
    if (e.ColumnIndex == 1)  // 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;
            }
        }
    }
}


This is a small Form with a Label (lblPrompt), a TextBox (txtResult) and two Buttons (btnOK, btnCancel). The FormBorderStyle is set to FixedDialog
C#
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;


        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 < 200)
        {
            size.Width = 200;
        }
        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;
        }
    }
}


The problem comes in trying to make the new value available for the 'Soup' button later. If you were only going to enter one new value and the Form with the Soup Button was on the calling form, OK. But there is no guarantee that the Soup Button Form is anywhere in the calling-chain for this operation and it is also likely that more than one new value will be entered at the same time, (Soup-10 and Olives-20 and AntiPasti-5 and so on). Therefore the only sensible approach says that these values should be stored somehow, so that when the Soup Button Form is opened the values are available.

Also if you are going to put the quantity on the buttons it would be better if it was available when the application first starts.

All of this stipulates that there must be some form of stock control mechanism in the application, even if it is as simple as adding a 'Qty' column to the food table, although this is not the best way to do it.

Let me know what you think.
Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!

GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 11:28
LAPEC6-Jan-11 11:28 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 11:29
Henry Minute6-Jan-11 11:29 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 11:32
LAPEC6-Jan-11 11:32 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 11:57
LAPEC6-Jan-11 11:57 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 12:00
Henry Minute6-Jan-11 12:00 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 12:04
LAPEC6-Jan-11 12:04 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 13:07
Henry Minute6-Jan-11 13:07 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 13:23
LAPEC6-Jan-11 13:23 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute6-Jan-11 14:19
Henry Minute6-Jan-11 14:19 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC6-Jan-11 15:13
LAPEC6-Jan-11 15:13 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute7-Jan-11 11:16
Henry Minute7-Jan-11 11:16 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC7-Jan-11 13:21
LAPEC7-Jan-11 13:21 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC9-Jan-11 8:37
LAPEC9-Jan-11 8:37 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute9-Jan-11 8:38
Henry Minute9-Jan-11 8:38 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC9-Jan-11 8:41
LAPEC9-Jan-11 8:41 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute9-Jan-11 9:10
Henry Minute9-Jan-11 9:10 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC30-Jan-11 4:13
LAPEC30-Jan-11 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.