Click here to Skip to main content
15,312,347 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi
I have a DataGridView that is populated with unbound data and im not using a DataView, I wish to disable editing on a row depending on a cell value in that row.

I am new to Windows Form Programming an im finding it hard to find any informaton on the subject.

Can anyone help

Here is the code (if it helps) that sets up the datagridview

private void button1_Click(object sender, EventArgs e)
        {
            String CountrySelect = Country.SelectedValue.ToString();
            SQLProcess defaults = new SQLProcess();
            DataSet info = defaults.DefaultCountryConfig(CountrySelect);
            
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.RowHeadersVisible = false;
            SetupColumns(info);
            
            dataGridView1.DataSource = info.Tables["Country"];
            
            Country.Enabled = false;
            button1.Enabled = false;
        }

    
        private void SetupColumns(DataSet info)
        {
            //Setup variables for gridview
           
            
            //Setup 
            DataGridViewTextBoxColumn FName = new DataGridViewTextBoxColumn();
            FName.DataPropertyName = "FName";
            FName.HeaderText = "Field Name";
            FName.ValueType = typeof(String);
            FName.Frozen = true;
            dataGridView1.Columns.Add(FName);
            SQLProcess Fieldtype = new SQLProcess();
            DataSet DTypes = Fieldtype.FieldTypes();
            
            DataGridViewComboBoxColumn FType = new DataGridViewComboBoxColumn();
            FType.DataPropertyName = "Ftype";
            FType.HeaderText = "Field Type";
            FType.DataSource = DTypes.Tables["FieldTypes"];
            FType.DisplayMember = "FieldType";          
            FType.ValueMember = "FieldTypeValue";
            FType.Frozen = true;
            FType.Width = 100;
            dataGridView1.Columns.Add(FType);
            
            DataGridViewTextBoxColumn Position = new DataGridViewTextBoxColumn();
            Position.DataPropertyName = "FPosition";
            Position.HeaderText = "Position";
            Position.ValueType = typeof(Int32);
            Position.Frozen = true;
            dataGridView1.Columns.Add(Position);
            
            DataGridViewCheckBoxColumn Requierd = new DataGridViewCheckBoxColumn();
            Requierd.HeaderText = "Requierd";
            Requierd.Frozen = true;
            Requierd.Width = 50;
            dataGridView1.Columns.Add(Requierd);
            
            DataGridViewCheckBoxColumn Option = new DataGridViewCheckBoxColumn();
            Option.HeaderText = "Option";
            Option.DataPropertyName = "Foption";
            Option.ValueType = typeof(String);
            Option.Frozen = true;
            Requierd.Width = 30;
            dataGridView1.Columns.Add(Option);
             
        }

thanks in advance.

[edit]Inline code converted to code block - OriginalGriff[/edit]
Posted
Updated 12-Apr-11 0:02am
v2

try this one

if(dataGridView1.CurrentRow.Cells["the name of the column you wish to check its value"].Value=="whatever the value to check")
{
   dataGridView1.currentRow.ReadOnly=true;
}


I hope I helped
:-)
   
Comments
xenosnoop 12-Apr-11 8:26am
   
Hi
Where would i put this in my code for it to work, i have tried in when in creating the Columns.

Im abit new to Windows forms

Many thanks for the reply everyone is being so helpful
xenosnoop 12-Apr-11 8:56am
   
Mayn thanks i have worked out where to put the code, thanks everyone for the help, much appreceated
xenosnoop 12-Apr-11 9:05am
   
Shouldent of spoken to soon im getting an error of Object refference not set to an instance of an object

any idea
a1mimo 12-Apr-11 9:11am
   
it seems to be that you putting this code in an area that being called before the columns created
so to avoid this error you may find it helpful to put the code between a try and catch

try
{
the code
}
catch
{
message or leave it empty doesnt matter
}
xenosnoop 12-Apr-11 9:35am
   
Yup thanks that worked, thanks
Hi xenosnoop,

dataGridView1["(your desired column)", dataGridView1.CurrentRow.Index].ReadOnly = dataGridView1["(the column to take the check value)", dataGridView1.CurrentRow.Index].Value;


I hope this help,
:)
   
v2
Comments
xenosnoop 12-Apr-11 6:52am
   
Hi Michael

Thanks for the quick responce, wow i would never thought of that, i have added the code but im getting an error

dataGridView1["Requierd", dataGridView1.CurrentRow].ReadOnly = dataGridView1["Option", dataGridView1.CurrentRow].Value;
The best overloaded method match for 'System.Windows.Forms.DataGridView.this[string, int]' has some invalid arguments

the errors in the error list says its expecting an int for the datagridview.CurrentRow
Michael Waguih 12-Apr-11 7:04am
   
Yes I was missing something in my code now ,I have improved it and it will works now
Good luck
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900