hi,
im still a novice in c# programming
im developing a windows form in visual studio 2010(krypton toolkit) in c# .net platform.
i have a datagrid view in my form named stage2grid which contains a column called layout id. when i make changes to this layout id column which is a string, i should be able to check if the particular changed value is already present in the database.
if yes then he should change the value else i should be able to continue with my work.
can u please help me put with the code.

Bellow is the simple code to check a particular value in datagridview... Were you asking for this type of solution?

if (dataGridView1.Rows[row_index].Cells[column_index].Value.ToString() == "value_compars")
{
                MessageBox.Show("Do Next Work");
//Rest of the code
}

can i check whether the value exists as soon as he changes the value in the datagrid i.e not based on an event like button click.
what i meant is there is a button in my application called load data. when i click this button the data is taken from the database and put into the datagrid.
now when i try makin changes to the value, can i check it as soon as i edit each row value of a particular column.

ill try out the code given thanks
please let me know if u have the solution for my doubt
thanks a lot

Do you populate dgv with all the "layout id" from the database? I means are the all rows from dataBase, present in the dgv?

And how do you populate dgv? Is it data bound, or not? If it is, you can loop through the dataTable of particular column, and check if the value exists or not. Then dedice what to do if the value is found or not.

i populate the layout id according to a particular pk_value i.e a number from a table called data6_checklistlayouts. i have to compare this layout id's with all layout id's of the table data6_checklistlayouts to check whether it exists

it is databound

string str== "select * from data6_checklistlayouts where pk_value=269";
dataset grid=objAccess.fetchfromDB(str);
datagridview1.datasource=grid.tables[0];

Hi, this is a bit more complicated as I thought. I knew I have to se CellValidating event, but there is quite some code, as you can see.
You can see Iam giving yo u a whole code I wrote, including with my example data (this one you can delete).

I have to say I tested it, and it works. Maybe there will appear any error after some time (Iam not 100% sure right now, that is all included), but it should work - at least for me it did!

This code as you can see has 2 columns (remember, this is only an example code, meant to show the point how it should work), 1st column is an Id (Column1), and there are numbers (it does not matter what it is, it could be the strings as well). And when you enter a new number into a new row, if this number is NOT yet included in the dataSource of dateGridView, it will all be OK. If this number already exists, you will get a warning. And you will be forced to insert a new one, which does not exist in the dataSource.

Take a look:

DataSet ds;
        string oldValue;
        public Form1()
        {
            InitializeComponent();            

            //
            //you can erase this code bellow, because you will create your own datSet!!
            //

            //creating dataSet (with dataTable):
            ds = new DataSet();
            DataTable table = new DataTable("MyTable");
            DataColumn[] columns = new DataColumn[]
            {
                new DataColumn("Column1", typeof(int)),
                new DataColumn("Column2", typeof(string))
            };
            table.Columns.AddRange(columns);
            ds.Tables.Add(table);

            //populating dataTable:
           
            DataRow dr;
            for (int i = 1; i < 8; i++)
            {
                dr = ds.Tables["MyTable"].NewRow();
                dr["Column1"] = i;
                dr["Column2"] = "Item " + i;
                table.Rows.Add(dr);
            }

            //
            //DO NOT DELETE THIS BELLOW!!
            //
            //binidng dataTable to the dgv:
            this.dataGridView1.DataSource = new BindingSource(ds.Tables["MyTable"], null);
            //create an event for the dgv to check the new value:
            this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
            this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            this.dataGridView1.AllowUserToAddRows = true; //use this code, if you always want to have a new row bellow!
        }
                
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (this.dataGridView1.IsCurrentCellDirty)
            {
                if (this.dataGridView1.Rows[e.RowIndex].IsNewRow) return;
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText != "Column1") return;

                // Confirm that the cell is not empty.
                if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    MessageBox.Show("Cell must not be empty. Please insert new Id.");
                    e.Cancel = true;
                }

                //loop through all the dataTable, to find the same value:
                //if found this will notify the user:               
                for (int i = 0; i < this.ds.Tables["MyTable"].Rows.Count; i++)
                {
                    string tableValue = this.ds.Tables["MyTable"].Rows[i]["Column1"].ToString();                    
                    if (e.FormattedValue.ToString() == tableValue)
                    {
                        if (e.FormattedValue.ToString() == oldValue) return;
                        else
                        {
                            //get old value from dataTable
                            //in case if the user enteres the old value back!
                            oldValue = this.ds.Tables["MyTable"].Rows[e.RowIndex]["Column1"].ToString();                             
                            dataGridView1.Rows[e.RowIndex].ErrorText = "The Id: \"" + tableValue + "\" has been found in the database.\nPlease insert a new Id.";
                            e.Cancel = true;
                            break;
                        }
                    }
                }
            }
        }

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // Clear the row error in case the user presses ESC.   
            dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
        }

Let me know what do you think. And please take some time to study it - if I took it to create this code only fo you ;)

okay i studied it.
i will test the code when i go to work on monday
thanks a lot
i will tell u if it is working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.