Datagrid-columns

How do you assign a default value to a column on the Form_Load event.

How do you get the selected value from a column with a combobox, and one with a tick box.

Thanks ...



Answer this question

Datagrid-columns

  • Bravo2007

    as said previously, to set the value:

    this.theDataGridView.Rows[RowIndex].Cells[ColumnName].Value = "your value";

    what happens when you do this does it work what is your code



  • omoeko

    Using vs 2005.

    I want to use the following but dont know how to set the value...

    DataGridViewTextBoxColumn ocol16 = (DataGridViewTextBoxColumn)dataGridView1.Columns[16];

    Putting the routine you suggested as a handler works for rows 0 and 2 but not for 1.


  • Jim Ward

    to set the value of a column:

    this.theDataGridView.Rows[RowIndex].Cells[ColumnName].Value = "your value";

    however it is possible the above is bad practice as you would not be entirely sure if the row/column would exist in the long term.

    As for where to place the selected row routine - wherever you like. Which approach are you using Selected row routine OR the rows_added event, where you are creating columns and assigned default values

    Either way, wherever you place the code is entirely up to you from where you would like to process the function



  • joeydj

    The event handler work well - I was sort of thinking of something like but I cant seem to set the value.

    DataGridViewTextBoxColumn ocol16 = (DataGridViewTextBoxColumn)dataGridView1.Columns[16];

    which is the event to place the selecte row routine


  • KitWest

    Not really, the selected column contains a combobox or a tickbox- under which event and how would I find the value.

    How would one assign the column default value in the Form_Load event - I want it to work the same for all rows.


  • Ahmad Mageed

    Problem: how to set a column default value

    Solution:

    //Load the column default value

    DataGridViewTextBoxColumn Col16 = (DataGridViewTextBoxColumn)dataGridView1.Columns[16];

    Col16.DefaultCellStyle.NullValue = "1";

    thanks everyone


  • Milos Kompjuteras

    to get the selected value from a column, i guess you could iterate through each row/column and check whether the column of the current row is selected.

    Or you could use the SelectedCells property, which will return back the collection of cells selected by the user, then go through each cell selected

    An example:



    DataGridViewCellCollection theSelectedCells = this.theDataGridView.SelectedCells;
    foreach (DataGridViewCell curCell in theSelectedCells)
    {
    MessageBox.Show("Current Column name: " + this.theDataGridView.Columns[curCell.ColumnIndex].Name + " and value is: " + curCell.Value.ToString());
    }

    To assign a default value, I guess you would have to tell the datagridview what row/column you want a default value:

    this.theDataGridView.Rows[Index].Cells[ColumnName].Value = SomeValue;

    does this help



  • slein

    hmmm - I dont think I follow. I mean, the value that will be selected from the combobox will be the cell value, which can be obtained from the code given. I havent tried it but it should be the same I believe.

    As for the assignment of default values, I guess when a new row has been created, you can add columns manually (programmatically) and assign your cell value as needed - take a look at the RowAdded event.



    //this line below will appear in the form_load event
    this.theDataGridView.RowsAdded += new DataGridViewRowsAddedEventHandler(theDataGridView_RowsAdded);

    this.theDataGridView.Rows.Add(); //this is adding a new row which will automatically fire the RowsAdded event


    void theDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
    //this event will fire automatically when a new row has been added to the datagridview.
    //so when a new row has been added, you can add columns and set their default values as you wish, example:

    this.theDataGridView.Columns.Add("Col1", "Col1");
    this.theDataGridView.Rows[e.RowIndex].Cells["Col1"].Value = "your default value";
    }

    Does this help



  • NoobestNoob

    Your code above to set a default value for all rows in a column, using an eventhandler - worked for row 0 and 2, it didnot work for line 1.

    I want to know why the following will not work:

    DataGridViewTextBoxColumn ocol16 = (DataGridViewTextBoxColumn)dataGridView1.Columns[16];

    ocol16.value = "1"; < this line causes an error>


  • Datagrid-columns