Click here to Skip to main content
15,887,821 members
Home / Discussions / C#
   

C#

 
GeneralRe: Getting network drives names issue Pin
Dave Kreskowiak14-Jan-11 4:00
mveDave Kreskowiak14-Jan-11 4:00 
AnswerRe: Getting network drives names issue Pin
jschell14-Jan-11 10:04
jschell14-Jan-11 10:04 
QuestionTreeView SelectedNode[0] Pin
S078413-Jan-11 21:14
S078413-Jan-11 21:14 
AnswerRe: TreeView SelectedNode[0] Pin
Mycroft Holmes13-Jan-11 21:27
professionalMycroft Holmes13-Jan-11 21:27 
AnswerRe: TreeView SelectedNode[0] Pin
Richard MacCutchan13-Jan-11 22:08
mveRichard MacCutchan13-Jan-11 22:08 
AnswerRe: TreeView SelectedNode[0] Pin
RaviRanjanKr16-Jan-11 2:28
professionalRaviRanjanKr16-Jan-11 2:28 
GeneralRe: TreeView SelectedNode[0] Pin
S078416-Jan-11 19:53
S078416-Jan-11 19:53 
QuestionC# How to get row value of DataGridView Pin
LAPEC13-Jan-11 12:46
LAPEC13-Jan-11 12:46 
Hello Everyone

Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType.

On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // make sure that the click was in the right column
            if (e.ColumnIndex == 2)
            {
                // Value is given 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.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...
                        
                    }
                }
            }
        } 


How can I get the FoodType value from that row?

Here is the code of my click event button...

(Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)

private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
        private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

        private void cmdStarters_Click(object sender, EventArgs e)
        {
            OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
            connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
            connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
            connBuilder.Add("Jet OLEDB:Engine Type", "5");

            // Food SQL Query
            string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
            using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
            {
                dataGridView1.Columns.Clear();
                dataGridView1.RowTemplate.Height = 60;
                //====================================\\
                dataGridView1.Visible = true;
                dataGridView2.Visible = false;
                try
                {
                    OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
                    OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
                    OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
                    
                    DataSet ds = new DataSet();
                    conn.Open();
                    foodType.Value = "Starter";
                    foodsDa.Fill(ds, "Food_table");

                    //add extra column structures to dataset
                    ds.Tables["Food_table"].Columns.Add(new DataColumn("Quantity In Stock", System.Type.GetType("System.Int32")));
                    ds.Tables["Food_table"].Columns.Add(new DataColumn("Status", System.Type.GetType("System.String")));
                    //loop through all the rows and add the data to the new columns dynamically
                    for (int i = 0; i < ds.Tables["Food_table"].Rows.Count; i++)
                    {
                        ds.Tables["Food_table"].Rows[i]["Quantity In Stock"] = 0;
                        ds.Tables["Food_table"].Rows[i]["Status"] = "<Always On Stock>";
                    }
                    
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = "Food_table";
                    
                    DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
                    this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
                    dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    this.dataGridView1.Columns[0].Width = 420;
                    this.dataGridView1.Columns[1].Width = 180;
                    this.dataGridView1.Columns[2].Width = 300;
                    this.dataGridView1.Columns[3].Width = 308;

                    conn.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex);
                }
            }
        }


Any help please would be very greatfull

Thanks in advance

Lapeci
AnswerRe: C# How to get row value of DataGridView Pin
Dalek Dave13-Jan-11 13:05
professionalDalek Dave13-Jan-11 13:05 
AnswerRe: C# How to get row value of DataGridView Pin
Luc 64801113-Jan-11 15:10
Luc 64801113-Jan-11 15:10 
AnswerRe: C# How to get row value of DataGridView Pin
Luc 64801113-Jan-11 15:14
Luc 64801113-Jan-11 15:14 
AnswerRe: C# How to get row value of DataGridView Pin
Dalek Dave13-Jan-11 15:33
professionalDalek Dave13-Jan-11 15:33 
AnswerRe: C# How to get row value of DataGridView Pin
Luc Pattyn13-Jan-11 15:50
sitebuilderLuc Pattyn13-Jan-11 15:50 
QuestionMost efficient Collection? Pin
kinar13-Jan-11 10:41
kinar13-Jan-11 10:41 
AnswerRe: Most efficient Collection? PinPopular
dan!sh 13-Jan-11 11:20
professional dan!sh 13-Jan-11 11:20 
AnswerRe: Most efficient Collection? Pin
Luc Pattyn13-Jan-11 11:28
sitebuilderLuc Pattyn13-Jan-11 11:28 
AnswerRe: Most efficient Collection? Pin
Ray Cassick13-Jan-11 11:54
Ray Cassick13-Jan-11 11:54 
AnswerRe: Most efficient Collection? Pin
SledgeHammer0113-Jan-11 13:00
SledgeHammer0113-Jan-11 13:00 
AnswerRe: Most efficient Collection? Pin
Dalek Dave13-Jan-11 13:08
professionalDalek Dave13-Jan-11 13:08 
GeneralOT [ sufficiently remedied for now ] Pin
Luc Pattyn13-Jan-11 13:15
sitebuilderLuc Pattyn13-Jan-11 13:15 
GeneralRe: Most efficient Collection? Pin
Dalek Dave13-Jan-11 13:18
professionalDalek Dave13-Jan-11 13:18 
GeneralRe: Most efficient Collection? Pin
Luc Pattyn13-Jan-11 13:23
sitebuilderLuc Pattyn13-Jan-11 13:23 
AnswerRe: Most efficient Collection? Pin
jschell14-Jan-11 10:09
jschell14-Jan-11 10:09 
QuestionHaving trouble writing to an excel file Pin
turbosupramk313-Jan-11 9:40
turbosupramk313-Jan-11 9:40 
AnswerRe: Having trouble writing to an excel file Pin
Dalek Dave13-Jan-11 10:31
professionalDalek Dave13-Jan-11 10:31 

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.