CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question DataGrid problems

    I will be very thankfull if you could help me by answering my questions according DataGrid:

    1. Each time I fill all fields in last row, a new row is being created (automatically). How to turn this feature off? (I would like the user who wants to add new row, to press a button "new" located outside DataGrid, to add a new row ).

    2. I want to allow the user to edit DataGrid (ReadOnly == false). How to get a data from the row, that has just been edited?

    3. I would like to make 2 columns - one ReadOnly == false, second ReadOnly == true. I tried in such way:

    Code:
     DataTable dt = new DataTable("Coordinates"); 
    
    this.dgPnt.DataSource = dt;
    dt.Columns.Add("X");
    
    dt.Columns[0].ReadOnly = true;
    but it doesn`t seem to work.

    thank you in advance for any help

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: DataGrid problems

    1. If your DataSource is a DataView object you can set its AllowNew property to false:

    Code:
     
    DataView dv = new DataView(myDataTable);
     
    dv.AllowNew = false;
     
    dataGrid1.DaatSource = dv;
    If the DataSource is a DataTable, you can use its DefaultView property to set the AllowNew property:

    Code:
     
    DataTable dt = myDataSet.Tables["mytable"];
     
    dt.DefaultView.AllowNew = false;
     
    dataGrid1.DataSource = dt;
    2. Whenever you change something in the Grid the value is automatically passed to the DataSource row when the user exit the edited row.
    An alternative way is to use the CurrentCellChanged event.
    3. You will need to use DataGridColumnStyles.
    Little explenation about ColumnStyles and a way to learn them:

    You need to use the TableStyles property of the DataGrid, which has a MappingName property (name of the table) and ColumnStyles property.
    Each ColumnStyle has a MappingName property which is the name of the column you want to show.

    In order to understand how to code this, I strongly suggest you use the designer, and then see what code was generated.

    Add a DataGrid. In the Properties Grid look for TableStyles property. Click on the elipsis button, and you get an Editor. Add one, and set the MappingName accordingly. Click on the ColumnStyles properties, and you can add a ColumnStyle for each column you want to show. Set the mapping name for each ColumnStyle. You can also set style for each column, but I will let you investigate on that.

    After you click ok look at the generated code. If you only added the columns you wanted to see, then this is what you get.

    Now, each DataGridColumnStyle has a ReadOnly property.
    Last edited by jhammer; October 2nd, 2005 at 09:18 AM.

  3. #3
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Re: DataGrid problems

    First, thank you for help.

    acording to 3) -> I tried to do everything as you said. I clicked into TableStyles and set one column with ReadOnly true and the other with false. (I can`t figure out what the mapping name is for exactly). Then I defined:

    Code:
     
    DataTable dt = new DataTable("Coordinates");
     
    
    and in OnLoad method:
    Code:
     dt.Columns.Add("X"); 
     
    dt.Columns.Add("Y");
     
    this.dataGrid1.DataSource = dt;
    still both columns are editable...
    Last edited by yoyosh; October 3rd, 2005 at 04:29 PM.

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: DataGrid problems

    The following code is working:
    Code:
    //Initializing
    dataGridTableStyle1 = new DataGridTableStyle();
    dataGridTextBoxColumn1 = new DataGridTextBoxColumn();
    dataGridTextBoxColumn2 = new DataGridTextBoxColumn();
    dataGrid1.BeginInit();
     
    //Creating DataSource
    DataTable dt = new DataTable("MyTable");
    dt.Columns.Add(New DataColumn("X",typeof(string));
    dt.Columns.Add(New DataColumn("Y",typeof(string));
    dataGrid1.DataSource = dt;
     
    // dataGridTableStyle1
    
    dataGrid1.TableStyles.AddRange(new DataGridTableStyle[] {dataGridTableStyle1});
    dataGridTableStyle1.DataGrid = dataGrid1;
    dataGridTableStyle1.GridColumnStyles.AddRange(new DataGridColumnStyle[] {dataGridTextBoxColumn1,dataGridTextBoxColumn2});
    dataGridTableStyle1.MappingName = "MyTable"; // This is the name of the table you are binding to.
     
    // dataGridTextBoxColumn1
    dataGridTextBoxColumn1.HeaderText = "XXX"; // This is the text that is written in the Column Header. Leave empty to show the name of the column.
    dataGridTextBoxColumn1.MappingName = "X"; // This is the name of the column.
    dataGridTextBoxColumn1.ReadOnly = true;
    dataGridTextBoxColumn1.Width = 75;
     
     
    // dataGridTextBoxColumn2
    dataGridTextBoxColumn2.HeaderText = "YYY"; 
    dataGridTextBoxColumn2.MappingName = "Y"; // This is the name of the column.
    dataGridTextBoxColumn2.ReadOnly = false;
    dataGridTextBoxColumn2.Width = 75;
     
    dataGrid1.EndInit();
    
    The column with header YYY should be editable. The column with header XXX will be read-only.
    Last edited by jhammer; October 9th, 2005 at 02:29 AM.

  5. #5
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Re: DataGrid problems

    thank you jhammer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured