JGLP

Hi
How can i access the events of a comboBox that is inside
of a cell (on dataGridView)

For example i have a dataGridView with one Row.
the first cell have a comboBox with "name", "date", and "age".
i need to make an event when i click "name" on the comboBox,
inside the second cell of the same row, will appear "paul".

thanks!!


Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

element109

This code checks to see if the last cell that was edited is empty, if so assigns a value.

I don't have a combo in my datagrid and have not used one yet so I don't have the exact code but you should be able to modify this to achieve what you are after.

Code Snippet

Private Sub DataGrid_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMain.CellEndEdit

With Me.dgvMain

Select Case e.ColumnIndex

Case 1

If String.IsNullOrEmpty(CStr(.Item(1, e.RowIndex).Value)) Then

.Item(1, e.RowIndex).Value = "paul"

Exit Select

End If

End Select

End With

End Sub





Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

JGLP

hi element!
i was using C++ , but i got your ideia, and it worked! thanks




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

JGLP

Hi again... i'm facing a new problem, related to the same subject.

I have two arrays:
array<String ^> ^name;
array<String ^> ^age;

name[0] = "paul";
name[1] = "john";
age[0] = "30";
age[
1] = "40";

And I have a dataGridView with multiple rows, and just two columns...
on column 1 I have a comboBox with:
this->option->Items->AddRange("name");
this->option->Items->AddRange("age");

on column2 i have an empty comboBox called res
And deppending on the choise of the first comboBox on column 1, i need
to add on the comboBox on column 2.

Example:
If i choose on comboBox thats on column1 the "name", on comboBox thats on
column2 need to appear the choises "paul" and "john".
If i choose "age", so the second comboBox need to be "30" and "40".

My question, is how can add strings on other comboBoxes looking first on
the previous comboBoxes (allways inside a dataGridView)

I have this code already:

[code]
private: System::Void dataGridView1_CellEndEdit(System:: Object^ sender, System::Windows::Forms:: DataGridViewCellEventArgs^ e) {
if(e->ColumnIndex == 1)
{
String^ s =dataGridView1->CurrentRow->Cells[0]->Value->ToString();
option->Items->Clear();
int line = e->RowIndex;
if(s=="name")
{
for(int i=0;i<name->Length;i++)
{
//this->res->Items->AddRange(name[ i ]);
/*this will write on all rows...i want to manipulate only the current row*/
}

}
else
if(s=="age")
{
for(int i=0;i<age->Length;i++)
{
// how can i write the age array on comboBox(column 2)
}

}
}
}
[code]

thanks very much for the help




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

JGLP

or putting the question in another way:

is possible to have a column with comboBoxes inside a cell (dataGridView) and
having diferent items for the comboBox in each row

example:
comboBox in column1:

row1: <a,b,c,d>
row2: <e,f,g,h> (same comboBox but with diferent items)





thanks in advance




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

element109

Code Snippet

Private Sub dgvMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMain.CellEndEdit

Select Case e.ColumnIndex

Case 0

With Me.dgvMain

Dim cmbo As System.Windows.Forms.DataGridViewComboBoxCell

cmbo = CType(.Rows.Item(e.RowIndex).Cells(1), DataGridViewComboBoxCell)

cmbo.Items.Clear()

Dim data() As Object = {"paul", "john"}

If .Rows.Item(e.RowIndex).Cells(0).Value.ToString = "Name" Then

data = New Object() {"paul", "john"}

Else

data = New Object() {"30", "40"}

End If

.Rows.Item(e.RowIndex).Cells(1).Value = Nothing

cmbo.Items.AddRange(data)

End With

End Select

End Sub





Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

JGLP

thanks element!!!!!!!!!!!




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

Phiban

would anyone have a c# version of the code Ive tried working it out but keep incurring errors and I have also tried it with a c# converter but still no joy Tongue Tied


Thanks in advance
Phil




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

element109

In order for this code to work you need your dgv to have the first two columns set to combobox cells, then you have to add name and age as default items in the combobox in the first column.

Code Snippet

private void dgvMain_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)

{

switch (e.ColumnIndex)

{

case 0:

System.Windows.Forms.DataGridViewComboBoxCell cmbo;

cmbo = ((System.Windows.Forms.DataGridViewComboBoxCell)(this.dgvMain.Rows[e.RowIndex].Cells[1]));

cmbo.Items.Clear();

object[] data;

if (this.dgvMain.Rows[e.RowIndex].Cells[0].Value.ToString() == "name" )

{

data = new object[] {"paul","john"};

}

else

{

data = new object[] {"30","40"};

}

this.dgvMain.Rows[e.RowIndex].Cells[1].Value = null;

cmbo.Items.AddRange(data);

break;

}

}

If you are still having problems post your error message.





Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

Phiban

Thanks element Smile

Worked perfectly Big Smile


Although I have another question which is sort of off topic, is there away to have the first row of the datagridview set to a default value so when the form loads with the datagridview the first row has the default values selected

Thanks again Big Smile




Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

element109

I'm assuming you are referring to the row that you click to add a new row, I think it is just a placeholder and cannot be initialized.

I like to turn it off and add buttons to add and remove rows. In the add button event you can easily set the new row to default values.

this.dgvMain.AllowUserToAddRows = false;

this.dgvMain.AllowUserToDeleteRows = false;





Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

Phiban

No,

When my form loads which has the datagridview it has one blank row (index 0), is there away i could set default values so when this loads the values are showing i.e. selected

Thanks for all ur help Big Smile





Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

shiras

i have a datagrid which has 8 columns, out of which first 3 columsn data are coming from a datatable. last 5 columns are dropdowns. now on_Load of the page lets guess there will be minimum 10 records. now the datagrid will look like this. first three coloumns will have data whcih is from my datatable. other 5 dropdowns will remain unselected. now i have written a selected index change to fill each dropdown.

what i want is while on_Load i want to fill some default values to the dropdown. for example if first dropdown is Country , second is state, third is district, fourth is ...............

now i want to select default to India (country) then Default to Karnataka as (state) and default Bangalore as district. how can i achieve this






Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

shiras

i have a datagrid which has 8 columns, out of which first 3 columsn data are coming from a datatable. last 5 columns are dropdowns. now on_Load of the page lets guess there will be minimum 10 records. now the datagrid will look like this. first three coloumns will have data whcih is from my datatable. other 5 dropdowns will remain unselected. now i have written a selected index change to fill each dropdown.

what i want is while on_Load i want to fill some default values to the dropdown. for example if first dropdown is Country , second is state, third is district, fourth is ...............

now i want to select default to India (country) then Default to Karnataka as (state) and default Bangalore as district. how can i achieve this

when we default to India (country) then State will get automatically populated...................






Re: Visual C++ Express Edition Events for ComboBox thats inside a dataGridView

JGLP

hi
i guess you can achive that by looking to element109 answer above:

If .Rows.Item(e.RowIndex).Cells(0).Value.ToString = "Name" Then

data = New Object() {"paul", "john"}

Else

data = New Object() {"30", "40"}

regards