i want to create combobox inside gridview on button click event...
each time when i cklick on button a combobox will be created in the next row ..means every time only row will change column will remains same...
private void Buttton_Click(object sender, Event e)
{
DataGridViewComboBoxCell CellColumn1, CellColumn2, CellColumn3;
dataGridView1.Columns.Add("Col1", "Column1");
dataGridView1.Columns.Add("Col2", "sanjeev");
//make row 1 at all columns into combobox cell
dataGridView1.Rows[j].Cells[0] = new DataGridViewComboBoxCell();
dataGridView1.Rows[j].Cells[1] = new DataGridViewComboBoxCell();
CellColumn1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[0];
CellColumn2 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[1];
j++;
}
i tried this logic ..this will create combox inside gridview only at first button click but when i click the button again this is not working..
can any one help me out....
finally i got the answer...
private void BTN_ADD_Click(object sender, EventArgs e)
{ int j=0;
DataGridViewComboBoxCell ColumnItem2 = new DataGridViewComboBoxCell(); // create a combobox cell
ColumnItem2.ValueMember = "Display";
ColumnItem2.DisplayMember = "Display";
for(int i=0;i<10;i++)// it will add elements to a combox
{
ColumnItem2.Items.Add(i);
}
dataGridView1.Rows.Add(); // add row everytime to add a new combobox to the next row.
int columncount = dataGridView1.ColumnCount;// count no of coloumns
int rowcount = dataGridView1.RowCount;;// count no of rows
//i kept count no of columns and rows so that by rows and columns count u can specify to which row or you want to add combobox inside gridview
// below i hard coded the combobox row and column to 2,2 so it will add a combobox to 2nd row and 2nd column.
dataGridView1[2, 2] = ColumnItem2;
//dataGridView1[2, j] = ColumnItem2;
// general way of adding combox to second column and row is everytime added by one as i m incrementing j value every time to each time on button click it will add combox on next row on second column..same way you can specify row and column as per your need.
j++;
}
hope this will help others....
http://csharpprobsandsoln.blogspot.in/2013/04/how-to-add-combobox-dynamically-to.html
Related
I can’t to get the Row Handle in the GridView by the index in the DataTable DataSource.
I need to pass the value at this comand: gridView3.FocusedRowHandle = ????here????;
in short, what I'm doing. I have two datagrids, the first one must show the row highlighted and filtered, when I select information in the second datagrid.
Image by Form
in this example, I selected a row in the second datagrid that contains the value "cod.uniforme" = 15.
So the line was highlighted in red in the first datagrid, but the course was pointed in the 5th row
private void FirstDataGrid_RowStyle(object sender, RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
string CodUniforme = View.GetRowCellDisplayText(e.RowHandle, View.Columns["CODUNIFORME"]);
if (CodUniforme == txtValorGridView.Text)
{
e.Appearance.BackColor = Color.Salmon;
e.Appearance.BackColor2 = Color.SeaShell;
e.HighPriority = true;
}
}
}
I have two gridviews. One gridview datasource is setting through the database. Now I want that when I click on the Row of the first gridview its first column value of the selected row should be set to the second gridview.
private void masterTab1_ItemSelected(object sender, object selectedValue)
{
for (int i = 0; i < 1; i++)
{
SerialNumberGrid.Columns.Add("SerialNumber");
SerialNumberGrid.DataSource = masterTab1.HeaderGrid.SelectedRows[i].Cells[0].Value.ToString();
}
}
I am doing it this way But its giving me the index out of range exception.
I suggest you to create a DataTable and add your selected cell's value to it as a DataRow !
DataTable table = new DataTable();
table.Columns.Add("yourField", typeof(string));
table.Rows.Add(masterTab1.HeaderGrid.SelectedRows[0].Cells[0].Value.ToString());
And set this table as your Second grid's datasourse .
SerialNumberGrid.DataSource = table ;
SerialNumberGrid.DataBind();
Edit
You should get your selected cell value likes
masterTab1.SelectedRows[0].Cells[0].Value.ToString();
or
masterTab1.SelectedRows[0].Cells[0].Text;
And you should know how to convert DataGridView selected rows to DataTable !
I have defined a datagridview with 5 columns and another with 1 column ..what i want to do it is on the click of the button in the datagridview with the columns, the row with the single column of the other datagridview pops up..is it possible?..this is the screenshot of my datagridview..
i want to add a datarow of another datagridview with only one column..i know how to handle cellevent but what i really want is the addition of datarow with only one column..
Hope this helps:
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
int newRow = dgv2.Rows.Add();
dgv2.Rows[newRow].Cells[0].Value = dgv1.Rows[e.RowIndex].Cells["Stringtext"].Value;
}
}
This should take the value from the StringText cell and place it in the first column of a newly created row in the second grid when the button in cell 4 is clicked in the first grid.
EDIT:
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
dgv1.Rows[e.RowIndex].Cells["Stringtext"].Value = dgv2.Rows[{y}].Cells[0].Value;
}
}
This one should set the value of the cell "StringText"(which can be changed to whatever you need it to be) in dgv1 to the value of the cell in dgv2 with the rowIndex {y}.
I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?
Many thanks!
There should be a "validating" event for you to intercept and cancel if the value is -1.
I am trying to move selected rows from one DGV to another DGV at the click of a button.
I really don't know where to begin..
I have two seperate DGVs that are bound by a DataSource..
choiceDGV.DataSource = theChoiceList;
universalDGV.DataSource = theUniversalList;
I would like to move any selected items in the choiceDGV to the univeralDGV by a click of a button. I need to make sure the selected rows are removed from the one DGV and added to the 2nd DGV.
Both of the DataGridView's have the same amount of columns.
Did you try:
foreach (DataGridViewRow row in choiceDGV.SelectedRows)
{
universalDGV.Rows.Add(row);
choiceDGV.Rows.Delete(row);
}
or (edited: DataGridViewRow doesn't have ItemArray unfortunately):
foreach (DataGridViewRow row in choiceDGV.SelectedRows)
{
object[] items = new object [row.Cells.Count];
for (int i = 0; i < row.Cells.Count; i++)
items[i] = row.Cells[i].Value;
universalDGV.Rows.Add(items);
choiceDGV.Rows.Delete(row);
}
I can't see the issue. You just need move the items you like to the UniversalList within the onclick button handler and call .DataBind() on both the datagridview.
In other words you have to hit the lists(datasource) instead of the data grid view