i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.
dgFilter is here datagridview
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = loadOperators();
dgcoSpec.DisplayMember = "Operatortxt";
dgcoSpec.ValueMember = "Operatorvalue";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Operators";
dgcoSpec.Name = "Operators";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 130;
dgFilter.Columns.Insert(1, dgcoSpec);
here this way i read data from combobox column
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}
but the problem is i am not getting the combox value member rather i am getting display member.
so how to extract value member from for loop. please guide me with code. thanks
The value member is the string that is displayed inside the DataGridViewComboboxCell.
The actual Combobox control only exists during the time frame in which the user is editing the cell.
If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
Console.WriteLine(index);
}
In this example, I am using -1 as an invalid value.
EDIT Just noticed you are using a DataSource;
See DataGridViewComboBoxColumn name/value how? for possible duplicate.
Related
I have following code to remove the summary row/band value on a retrieve UI event. I'm certain this is the wrong way to do it but it works.
public UiEventResult AfterRetrieveData_111(object sender, RetrieveDataEventArgs e)
{
UltraGridBase baseGrid = _view.ViewGrids["ultraGrid1"];
UltraGridLayout gridLayout = baseGrid.DisplayLayout;
for (int i = 0; i < 2; i++)
{
gridLayout.Bands[i].Columns["columnA"].Formula = "''";
}
for (int i = 0; i < 3; i++)
{
gridLayout.Bands[i].Columns["columnB"].Formula = "''";
gridLayout.Bands[i].Columns["columnC"].Formula = "''";
}
Is there a way to program the retrieve so that it populates the summary row for column A/band[2] so that is uses the last value in each column? Without the above code it will sum rows under but would like for a way for it to use the last row value instead. Data will always be sorted DESC by date so last row will always be the value needed...
One way to achieve this is in InitializeRowEvent by setting the value of the columnA to the value of the last row in the child band like this:
// Update the rows only in the first band. You can also use e.Row.Band.Key == {YOU_BAND_KEY}
if (e.Row.Band.Index == 0)
{
// set the value of the columnA to the value of the last row in the child band
e.Row.Cells["columnA"].Value = e.Row.ChildBands.LastRow.Cells["columnA"].Value;
}
Note, this will not work if you edit the cells values. If you need to update the parent row value after cell update, again in InitializeRowEvent you can add this:
// look for row in the secon band
if (e.Row.Band.Index == 1)
{
// get the last row in the second band
if (e.Row == e.Row.ParentRow.ChildBands.LastRow)
{
// get the value of the last row in the second band and set it to the parent row
e.Row.ParentRow.Cells["columnA"].Value = e.Row.Cells["columnA"].Value;
}
}
This will loop through ChildBands and set parent row value with the last value in each ChildBand.
int rowCount = gridLayout.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
foreach (UltraGridChildBand childBand in baseGrid.Rows[i].ChildBands)
{
foreach (UltraGridRow row in childBand.Rows)
{
row.Cells["columnA"].Value =row.ChildBands.LastRow.Cells["columnA"].Value;
}
}
}
I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.
Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.
So far i have wrote code upto this point. But it always giving me First Item of a Combobx.
DataGrid gd = (DataGrid)sender;
DataRowView rowSelected = gd.SelectedItem as DataRowView;
if(rowSelected!=null)
{
for (int i = 0; i < comboBox1.Items.Count;i++ )
{
if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
}
comboBox1.SelectedItem= comboBox1.Items[index];
}
textBox9.Text=rowSelected[14].ToString();
}
Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.
for (int i = 0;i <comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = i;
}
}
comboBox1.SelectedIndex = index;
Change your code to
if(rowSelected!=null)
{
int index = comboBox1.Items.IndexOf(rowSelected[14]);
comboBox1.SelectedItem = comboBox1.Items[index];
}
or
Use FindStringExact() method of combobox
int i = comboBox1.FindStringExact("Combo");
if(i >= 0)
{
}
I want to select column from data grid view.
Then, I want to add data in cells number 5 for the selected column.
Below code is not worked.
for (int i = 0; i < dgvSRP.Rows.Count; ++i)
{
dgvSRP.Rows.Add();
DataGridViewRow row = this.dgvSRP.Rows[i];
row.Cells[5].Value =txtJumlahPEsanan.Text;
}
foreach(var dgvRow in dgvSRP.Rows)
{
dgvRow.Cells[4].Value = txtJumlahPEsanan.Text; // The 5th column is index 4. Indexes always start at 0.
}
Here is my code i have select specific column value and assign it to textbox. May this solve your problem. Remember i have two cells rows thats why i kept cell[1] because index start from 0.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == 5)
{
textBox3.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
In the below code i have a session value in which i have to pass to the grid and bind the values.The grid consists of textboxes if the session values is 2 there should be two row of textbox.I tried it throws index was out of range.Pls help me to over this issue.
int GoodsReceivedNoteID = (int)Session["GoodsReceivedNoteID"];
for (int iRow = 0; iRow < GoodsReceivedNoteID; iRow++)
{
TextBox txtFreightCharges = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtFreightCharges");
TextBox txtLoadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtLoadingCost");
TextBox txtUnloadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtUnloadingCost");
TextBox txtInsuranseCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtInsuranseCost");
TextBox txtOtherExpenses = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtOtherExpenses");
}
Don't use Rows[index].Cells[6].FindControl, the cell is not the NamingContainer of a control in a GridViewRow's TemplateField but the row itself. I also don't understand the relation between your session value and the number of rows in the grid. This is simpler and more readable:
foreach(GridViewRow row in gvGRN.Rows)
{
TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
TextBox txtLoadingCost = (TextBox)row.FindControl("txtLoadingCost");
TextBox txtUnloadingCost = (TextBox)row.FindControl("txtUnloadingCost");
TextBox txtInsuranseCost = (TextBox)row.FindControl("txtInsuranseCost");
TextBox txtOtherExpenses = (TextBox)row.FindControl("txtOtherExpenses");
}
If you only want to take GoodsReceivedNoteID-rows (which sounds wrong since an ID is not a counter):
for(int i = 0; i < GoodsReceivedNoteID; i++))
{
GridViewRow row = gvGRN.Rows[i];
TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
// ...
}
I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.