As described in the topic, I'm trying to add a new Row to my Datagridview.
In the Constructor of the form I'm setting AllowUserToAddRows to false.
I'm still able to add the row programmatically but it doesn't seem to get saved in my Settings File.
Here's the code of my form - I left some (hopefully not essential) parts out:
P.S.: pay attention to my comment at the end of my btnAddEntry_Click()-Method
public DataSettings()
{
InitializeComponent();
//Import rows that are saved int settings
for (int i = 0; i < Properties.Settings.Default.colNames.Count; i++)
{
dgv.Rows.Add(new DataGridViewRow());
dgv.Rows[i].Cells[0].Value = Properties.Settings.Default.colNames[i];
dgv.Rows[i].Cells[1].Value = Properties.Settings.Default.colStarts[i];
dgv.Rows[i].Cells[2].Value = Properties.Settings.Default.colWidths[i];
}
//Hide "new row"-row
dgv.AllowUserToAddRows = false;
}
private void cancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.colNames = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.colStarts = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.colWidths = new System.Collections.Specialized.StringCollection();
foreach (DataGridViewRow row in dgv.Rows)
{
if (row.Index < dgv.Rows.Count - 1)
{
Properties.Settings.Default.colNames.Add((String)row.Cells[0].Value);
Properties.Settings.Default.colStarts.Add((String)row.Cells[1].Value);
Properties.Settings.Default.colWidths.Add((String)row.Cells[2].Value);
}
}
Properties.Settings.Default.Save();
this.DialogResult = DialogResult.OK;
}
private void btnAddEntry_Click(object sender, EventArgs e)
{
dgv.AllowUserToAddRows = true;
Dialogs.Data_AddRow newRow = new Dialogs.Data_AddRow();
newRow.ShowDialog();
dgv.Rows.Add(new string[] { newRow.parmName, newRow.parmStart, newRow.parmWidth });
newRow.Dispose();
dgv.AllowUserToAddRows = false; //If I comment out this line - It works fine.
//but then the "newrow"-row is visible
}
private void btnDeleteEntry_Click(object sender, EventArgs e)
{
dgv.Rows.Remove(dgv.SelectedRows[0]);
}
private void btnDeleteAll_Click(object sender, EventArgs e)
{
dgv.Rows.Clear();
}
You are losing info of last row because of this line: (row.Index < dgv.Rows.Count - 1) should be (row.Index < dgv.Rows.Count) or just get rid of it.
If you want to check if the last row is not NewRow when saving do this:
foreach (DataGridViewRow row in dgv.Rows)
{
if (!row.IsNewRow)
{
Properties.Settings.Default.colNames.Add((String)row.Cells[0].Value);
Properties.Settings.Default.colStarts.Add((String)row.Cells[1].Value);
Properties.Settings.Default.colWidths.Add((String)row.Cells[2].Value);
}
}
Related
I try:
private int GetRowHandleByColumnValue(GridView view, string ColumnFieldName, object value)
{
int result = GridControl.InvalidRowHandle;
for (int i = 0; i < view.RowCount; i++)
if (Equals(value, view.GetRowCellValue(i, ColumnFieldName)))
return i;
return result;
}
private void Locate()
{
int rowHandle = GetRowHandleByColumnValue(dgvMedici, "Parafa", txtParafa.Text);
if (rowHandle != GridControl.InvalidRowHandle)
{
dgvMedici.FocusedColumn = dgvMedici.Columns.ColumnByFieldName("Parafa");
dgvMedici.FocusedRowHandle = rowHandle;
dgvMedici.ShowEditor();
}
else
MessageBox.Show("Not found!");
}
private void dgvMedici_RowCellStyle(object sender, RowCellStyleEventArgs e)
{
GridView view = sender as GridView;
if (view.FocusedRowHandle == e.RowHandle && !view.FocusedColumn.Equals(e.Column))
e.Appearance.BackColor = Color.Orange;
}
it's possible to exist a function like Locate from Delphi in C#?
You can change colors and other row styles by using DefaultCellStyle property.
private void YourEvent(object sender, EventArgs e)
{
DataGridViewRow row = new DataGridViewRow(); // an example row
row.DefaultCellStyle.BackColor = System.Drawing.Color.Orange; // change back color to orange
}
How can I fix the previous button to move backward in dataGridView row?
The next button is working.
It does nothing.
I don't know how to fix that. Any ideas? I would appreciate it.
Here is my code:
int nRow;
private void Form1_Load
{
nRow = DataGridView2.CurrentCell.RowIndex;
}
private void PreviousData_Click
{
if (row>=0)
{
if (row!=0)
{
DataGridView2.Rows[row].Selected = false;
DataGridView2.Rows[--row].Selected = true;
}
else
{
MessageBox.Show("Need More Data!");
}
}
}
int row;
private void DataGridView2_CellClick
{
if (DataGridView2.SelectedRows.Count != -1)
{
row = DataGridView2.CurrentRow.Index;
}
}
Bind the following two events to your previous and next buttons, respectively and it will do the job.
private void PreviousData_Click(object sender, EventArgs e)
{
var currentRow = DataGridView2.CurrentCell.RowIndex;
if (currentRow == 0)
return;
else
currentRow--;
DataGridView2.ClearSelection();
DataGridView2.CurrentCell = DataGridView2.Rows[currentRow].Cells[0];
DataGridView2.Rows[currentRow].Selected = true;
}
private void NextData_Click(object sender, EventArgs e)
{
var currentRow = DataGridView2.CurrentCell.RowIndex;
if (currentRow == DataGridView2.RowCount - 1)
return;
else
currentRow++;
DataGridView2.ClearSelection();
DataGridView2.CurrentCell = DataGridView2.Rows[currentRow].Cells[0];
DataGridView2.Rows[currentRow].Selected = true;
}
We have a very strange problem in a Windows Form that we cannot seem to figure out.
Our Windows Form has a DataGridView with a DataGridViewCheckBoxColumn in the first column.
We've added a the following functionality that allows a user to shift->click to select multiple rows in this grid:
int colHit = gvLibrary.HitTest(e.X, e.Y).ColumnIndex;
int lastRowHit;
//mouse left click
if (e.Button == MouseButtons.Left)
{
if (colHit == 0)
{
if (Control.ModifierKeys == Keys.Shift)
{
lastRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
ShiftClickCheckBoxSetter(this.gvLibrary, int.Parse(txtFirstClickRow.Text), lastRowHit);
}
else
{
int firstRowHit = gvLibrary.HitTest(e.X, e.Y).RowIndex;
txtFirstClickRow.Text = firstRowHit.ToString();
}
}
}
Here's the CheckBoxSetter Code:
private void ShiftClickCheckBoxSetter(DataGridView dataGridView, int p, int lastRowHit)
{
if (p < lastRowHit)
{
for (int i = p; i < lastRowHit; i++)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
else//
{
for (int i = p; i >= lastRowHit; i--)
{
dataGridView.Rows[i].Cells[0].Value = true;
}
}
}
And this is working as expected.
We've also added a ContextMenuStrip to the control for a right-click event.
else if (e.Button == MouseButtons.Right)
{
if (colHit != 0)
{
ContextMenuStrip m = new ContextMenuStrip();
m.Items.Add("Select All", null, m_LibraryItemClicked);
m.Items.Add("Select None", null, m_LibraryItemClickedNone);
m.Show(gvLibrary, e.Location);
}
}
Delegate Event One:
void m_LibraryItemClicked(object sender, EventArgs e) {
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected) {
dgvr.Selected = false;
}
dgvr.Cells["LSelect"].Value = true;
}
}
Delegate Event Two:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
This allows to the user to select all or select none for the checkboxes.
When the Select All selection is chosen, all check boxes are checked:
However when the Select None option is selected:
All check boxes are de-selected, except for the last one checked in the Shift-Click event:
I would think that iterating through all of the Grid Rows and setting the checkbox to not selected would suffice, IE:
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
However there seems to be some kind of state property that is disallowing this checkbox in that row to be changed.
Thanks in advance.
I checked your code and could reproduce this behaviour. The problem seem to be with the current cell (not the cell selected). When you try to change this particular cell, the action doesn't get executed immediately.
To change this behaviour add a dataGridView1.CurrentCell = null; before changing the value of the "LSelect" cell. This should fix your issue.
private void m_LibraryItemClickedNone(object sender, EventArgs e)
{
dataGridView1.CurrentCell = null;
foreach (DataGridViewRow dgvr in gvLibrary.Rows)
{
if (dgvr.Selected)
dgvr.Selected = false;
dgvr.Cells["LSelect"].Value = false;
}
}
i have a dowpdownlist, a button and a checkbox inside the datagridview.
i just only manually created a check box column on the datagridview.
(here is the code)
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
DataGrid1.Columns.Add(CheckboxColumn);
here is the procedure.
step 1: the user will choose item on the checkbox.
step 2: the user will choose item on the dropdown.
Step 3: the user will click on the button and it will change the itemname
on the checkbox prior to the item selected on the dropdownlist.
here is my problem
after clicking on the button, nothings happen.
here is my code.
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
if (chk.Selected)
{
// codes here
}
else
{
//code here
}
}
x = x + 1;
}
* EDITED **
I've tested this and it definitely Works. Copy and paste this into a new project and play with it. It should help you get to where you need to be.
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn(true);
checkBox.HeaderText = "T/F";
dataGridView1.Columns.Add(checkBox);
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
if (Convert.ToBoolean(chk.Value) == true)
{
MessageBox.Show("Value Is True");
}
}
}
First thing that I would recommend to call:
DataGrid1.EndEdit();
Since, I have experienced that sometimes input do not appears as expected if this line is missing before retrieving a value of checkbox from the grid column.
So something like this:
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
if (chk.Value)
{
// codes here for checked condition
}
else
{
//code here for UN-checked condition
}
}
x = x + 1;
}
I have a C# winforms application and I am trying to get a button working that will select the next row in a datagridview after the one curently selected.
The code I have so far is:
private void button4_Click(object sender, EventArgs e)
{
try
{
Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
// index out of range on this line
dataGridView1.Rows[dataGridView1.SelectedRows[selectedRowCount].Index].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = selectedRowCount + 1;
}
catch (Exception ex)
{
return;
}
But on running this it throws an exception. Could anyone point out where I may be going wrong. The thrown error is: Index is out of range
try this:
int nRow;
private void Form1_Load(object sender, EventArgs e)
{
nRow = dataGridView1.CurrentCell.RowIndex;
}
private void button1_Click(object sender, EventArgs e)
{
if (nRow < dataGridView1.RowCount )
{
dataGridView1.Rows[nRow].Selected = false;
dataGridView1.Rows[++nRow].Selected = true;
}
}
First, set "Multiselect" property of datagridview to false.
int currentRow = dataGridView1.SelectedRows[0].Index;
if (currentRow < dataGridView1.RowCount)
{
dataGridView1.Rows[++currentRow].Selected = true;
}
It will select the next row in the datagridview.
Select Row and Cell for better solution.
This solution move row indicator on DataGridView.
private void _GotoNext(object sender, EventArgs e)
{
int currentRow = DataGridView1.SelectedRows[0].Index;
if (currentRow < DataGridView1.RowCount - 1)
{
DataGridView1.Rows[++currentRow].Cells[0].Selected = true;
}
}
private void _GotoPrev(object sender, EventArgs e)
{
int currentRow = DataGridView1.SelectedRows[0].Index;
if (currentRow > 0)
{
DataGridView1.Rows[--currentRow].Cells[0].Selected = true;
}
}
It's here:
dataGridView1.SelectedRows[selectedRowCount]
If you have 3 selected rows then selectedRowCount = 3 and there are three rows with indexes: 0, 1, 2.
You are trying to access #3 which doesn't exist.
this example to read value cell or column is number 4 of datagridview
int courow = dataGridView1.RowCount-1;
for (int i=0; i < courow; i++)
{
MessageBox.Show(dataGridView1.Rows[i].Cells[4].Value.ToString());
}
I prefer this row selection :
First check if no multiselect : number_of_data
Then get the select cell (or row) : row_index
private void next_click(object sender, EventArgs e)
{
int number_of_data = dataGridView.SelectedRows.Count;
if (number_of_data > 1) return;
int row_index = dataGridView.SelectedCells[0].RowIndex;
if (row_index < dataGridView.RowCount-1)
{
dataGridView.Rows[row_index++].Selected = false;
dataGridView.Rows[row_index].Selected = true;
}
// Do something
}
enter code here private void Form1_Load(object sender, EventArgs e)
{
X = dataGridView1.CurrentCell.RowIndex;//int x;
}
enter code here private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
this.dataGridView1.ClearSelection();
dataGridView1.Rows[0].Selected = true;
}
}
enter code here private void button3_Click(object sender, EventArgs e)
{
if (X < dataGridView1.RowCount)
{
if (X != dataGridView1.RowCount - 1)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[++X].Selected = true;
}
else
{
button2_Click(sender, e);//this else with make it loop
X = 0;
}
}
}
dgv_PhotoList.Rows[dgv_PhotoList.CurrentRow.Index+1].Selected = true;