How do you make a specific column in a datagrid readonly?

How do you make a specific column in a datagrid readonly I have a datagrid with 12 columns and I want to be able to make certain columns readonly.

I binded my datagrid to a datatable and already created the columns and the rest of the fields are populated.

col1, col2, col2, .......

please help, thanks ahead

Chan




Answer this question

How do you make a specific column in a datagrid readonly?

  • Christopher Yager

    I understand, I am pretty new at C# as well.

    In my previous example, what I did was create a new table style.

    DataGridTableStyle DcStyle=new DataGridTableStyle();

    and then you can set a lot of different features. You will see I am setting the width in this to 0 if I want them hidden or whatever number you want.

    DataGridTableStyle DcStyle=new DataGridTableStyle();

    DcStyle.HeaderForeColor=Color.White;

    DcStyle.HeaderBackColor=System.Drawing.ColorTranslator.FromHtml("93, 51, 127");

    DcStyle.HeaderFont= new Font("Tahoma", 8, FontStyle.Bold);

    foreach(DataColumn dc in ds.Tables["insurance_table"].Columns)

    {

    DataGridTextBoxColumn textColumn=new DataGridTextBoxColumn();

    textColumn.MappingName= dc.ColumnName;

    textColumn.HeaderText = dc.Caption;

    if(dc.ColumnName.ToString()=="id_insurance_table")

    {

    textColumn.ReadOnly = true;

    textColumn.Width=1;

    }

    if(dc.ColumnName.ToString()=="id_employee")

    {

    textColumn.ReadOnly = true;

    textColumn.Width=0;

    }

    if(dc.ColumnName.ToString()=="company")

    {

    textColumn.HeaderText="Company";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    if(dc.ColumnName.ToString()=="is_archived")

    {

    textColumn.Width=0;

    textColumn.ReadOnly = true;

    }

    DcStyle.GridColumnStyles.Add(textColumn);

    DcStyle.MappingName=ds.Tables["insurance_table"].TableName;

    dgInsurance.TableStyles.Add(DcStyle);

    dgInsurance.DataSource=ds.Tables["insurance_table"].DefaultView;

    } //End For Loop

    There is so many ways of doing things; which makes it good but sometimes difficult as well to find good examples! :-)



  • BManager

    Thanks for the help. I figured out how to make certain columns to be readonly. But I had to do this to the DataTable before its even binded to the dataGrid. Is there a
    better way to do this Also is there a way to set the width of each column I looked at the code above. But I think were using differnt types of datasource. Im
    a newby with C# so excuse me if my questions seem kinda stupid......


    Im not sure how to make the code you posted above to work favorable in mly situation. Are you binding your dataGrid to a datatable like me or some other type of datasource
    Below is my Code. Bascially its a simple windows form that ask the user to select a TSV (Tab Seperated Value) file. Reads its and puts it into an arrary.
    Then puts that arrary into a datatable. Then that datatable is binded to the datagrid. So far it opens and reads all the data very nicely in the Datagrid but
    Im not sure how to specify the width of each particular column.


    // Function to read the TSV File into a string

    private string Read(string file)
    {
    StreamReader reader = new StreamReader(file);
    string data = reader.ReadToEnd();
    reader.Close();

    return data;
    }



    // The DataGrid Will be populated when a user opens a TSV file in the openfiledialog and populate the datagrid

    private void btnDynamic_Click(object sender, System.EventArgs e)
    {

    // Just an open file Dialog that ask the user for a TSV file.
    // I temporarily store the data thats read from the file into a RichTextBox called rtb1 before its parsed by the
    // Regular Expression Below.
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
    //string datastring = "";
    string data = Read(openFileDialog1.FileName);
    rtb1.Text = data;

    // Show the name of the file in the form's caption.
    this.Text = String.Concat("Open Text File to Read (" + openFileDialog1.FileName + ")");
    }
    else
    {
    //Do nothing
    }


    // Regular expression to seperate the TSV values into an arrary of strings seperated by TABS
    Regex rex = new Regex("\t|\r|\n");
    string[] test = rex.Split(rtb1.Text);
    //int i = 0;

    // Just Debug Code ignore
    int aryLength = test.GetLength(0);
    System.Diagnostics.Debug.WriteLine(aryLength);


    // Create a two dimensional arrary from the 1 dimensional arrary so it will look more like a table. Creates a new row after every
    // Twelvth value, hence 12 columns in my table. About 133 rows.

    string[,] myString1 = new string[133,12];
    int aryIndex = 0;

    for (int row = 0; row < 133; row++)
    {
    for (int columns = 0; columns < 12; columns++)
    {
    myString1[row, columns] = test[aryIndex];
    aryIndex++;
    System.Diagnostics.Debug.WriteLine(test[aryIndex]);
    }
    }

    // Create a new DataTable and name and add the columns. Also sets which columns will be readonly
    // TRYING TO FIGURE OUT HOW TO SET THE WIDTH OF EACH COLUMN...STILL CANT FIGURE IT OUT...

    DataTable name = new DataTable("name");
    name.Columns.Add("System");
    name.Columns["System"].ReadOnly = true;
    name.Columns.Add("Teradyne SN");
    name.Columns.Add("Vendor SN");
    name.Columns.Add("PN");
    name.Columns["PN"].ReadOnly = true;
    name.Columns.Add("BD_Rev");
    name.Columns.Add("Slot");
    name.Columns["Slot"].ReadOnly = true;
    name.Columns.Add("Location");
    name.Columns["Location"].ReadOnly = true;
    name.Columns.Add("Firmware");
    name.Columns.Add("Empty");
    name.Columns["Empty"].ReadOnly = true;
    name.Columns.Add("GPIB");
    name.Columns["GPIB"].ReadOnly = true;
    name.Columns.Add("InSYS");
    name.Columns["InSys"].ReadOnly = true;
    name.Columns.Add("Description");
    name.Columns["Description"].ReadOnly = true;


    // Puting the values from the 2D arrary into the DataRow of the DataTable, 1 row at a time.

    for(int row = 0; row < 133; row++)
    {

    DataRow dr = name.NewRow();
    dr["System"]= myString1[row, 0];
    dr["Teradyne SN"]= myString1[row, 1];
    dr["Vendor SN"]= myString1[row, 2];
    dr["PN"]= myString1[row, 3];
    dr["BD_Rev"]= myString1[row, 4];
    dr["Slot"]= myString1[row, 5];
    dr["Location"]= myString1[row, 6];
    dr["Firmware"]= myString1[row, 7];
    dr["Empty"]= myString1[row, 8];
    dr["GPIB"]= myString1[row, 9];
    dr["InSys"]= myString1[row, 10];
    dr["Description"]= myString1[row, 11];
    name.Rows.Add(dr);
    }


    // Binding the DataTable to the Datagrid
    dataGridView1.DataSource = name;
    dataGridView1.ReadOnly = false;






    }



  • PatrickRyu

    Here is one way I do it in my programs: This is the whole block of code, but you can see the line in red is how you would make that individual column read only.

    DataGridTableStyle DcStyle=new DataGridTableStyle();

    DcStyle.HeaderForeColor=Color.White;

    DcStyle.HeaderBackColor=System.Drawing.ColorTranslator.FromHtml("93, 51, 127");

    DcStyle.HeaderFont= new Font("Tahoma", 8, FontStyle.Bold);

    foreach(DataColumn dc in dataSet2.Tables["patient"].Columns)

    {

    /* like above this will help you to define formatting to

    your feild values. which is bounding to DataGridTextBoxColumn

    using the Mappingname and HeaderText.*/

    DataGridTextBoxColumn textColumn=new DataGridTextBoxColumn();

    textColumn.MappingName= dc.ColumnName;

    textColumn.HeaderText = dc.Caption;

    if(dc.ColumnName.ToString()=="title")

    {

    /*this will change the caption of your

    datagrid Column's default value*/

    /*to the below mwntioned value.*/

    textColumn.Width=0;

    /*this will help you to align the content for you.*/

    //textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Right;

    /*this will help you set the back color of selected cell*/

    //textColumn.TextBox.BackColor=Color.Red;

    /*this will help you to adjest the width of Column's*/

    //textColumn.Width=75;

    }

    if(dc.ColumnName.ToString()=="lname")

    {

    textColumn.HeaderText="Last Name";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    if(dc.ColumnName.ToString()=="mname")

    {

    textColumn.Width=0;

    }

    if(dc.ColumnName.ToString()=="fname")

    {

    textColumn.HeaderText="First Name";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    if(dc.ColumnName.ToString()=="suffix")

    {

    textColumn.Width=0;
    textColumn.ReadOnly = true;

    }

    if(dc.ColumnName.ToString()=="ssn")

    {

    textColumn.HeaderText="SSN";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    if(dc.ColumnName.ToString()=="dob")

    {

    textColumn.HeaderText="DOB";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    if(dc.ColumnName.ToString()=="age")

    {

    textColumn.HeaderText="Age";

    textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Center;

    }

    /*this will help you from updating data content.*/

    /*finally we will add our formated column

    to DataGridTableStyle instance*/

    DcStyle.GridColumnStyles.Add(textColumn);

    /*Here you need to map the name of the table

    to mappingName property of datadridtablestyle instance */

    DcStyle.MappingName=dataSet2.Tables["patient"].TableName;

    /*then add your datadridtablestyle instance to

    datagrid TableStyles collection using Add() method.*/

    dataGrid1.TableStyles.Add(DcStyle);

    /*our normal databinding*/

    dataGrid1.DataSource=dataSet2.Tables["patient"].DefaultView;

    }



  • How do you make a specific column in a datagrid readonly?