Scene
Use DataGridView in Winform to complete adding a line, deleting a line, moving up a line, moving down a line
Complete
Add a line
private void TaskViewEditHelper_OnAddStep (object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow ();
dr.CreateCells (this.dataGridView_sk );
dr.Cells [0] .Value = "public number" + this.dataGridView_Task_ViewEdit.Rows.Count;
dr.Cells [1] .Value = "Linkou";
dr.Cells [2] .Value = "many programming tutorials And resources ";
//this.dataGridView_Task_ViewEdit.Rows.Insert(0, dr); // The added line is the first line of the list
this.dataGridView_Task_ViewEdit.Rows.Add (dr); // The added line is the last line
}
Delete a line
private void TaskViewEditHelper_OnRemoveStep (object sender, EventArgs e)
{
if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show ("Please select the deletion step first, click the first column to select the row");
}
else
{
if (XtraMessageBox.Show ("Are you sure you want to delete the selected step?") == System.Windows.Forms.DialogResult.OK)
{
foreach (DataGridViewRow dr in this.dataGridView_Task_ViewEdit.SelectedRows)
{
if (dr.IsNewRow == false)
{
// Assuming it is not a submitted row, after adding a row of data successfully by default, DataGridView creates a new row as the penetration position for new data
this.dataGridView_Task_ViewEdit.Rows.Remove (dr);
}
}
}
}
}
Move up one row
private void TaskViewEditHelper_OnUpStep (object sender, EventArgs e)
{
if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show ("Please select a row first, click the first column to select the row");
}
else
{
if (this .dataGridView_Task_ViewEdit.SelectedRows [0] .Index <= 0)
{
XtraMessageBox.Show ("This line is at the top, can't be moved up again!");
}
else
{
// Be careful: here is a good or bad way to bind the data transitional
// selected line number
int = selectedRowIndex GetSelectedRowIndex (this.dataGridView_Task_ViewEdit);
IF (selectedRowIndex> =. 1)
{
// imitation selected row
the DataGridViewRow the newRow = dataGridView_Task_ViewEdit.Rows [selectedRowIndex];
// deleting the selected line
dataGridView_Task_ViewEdit.Rows.Remove (dataGridView_Task_ViewEdit.Rows [selectedRowIndex]);
// the imitation line, to pierce the selected row position
dataGridView_Task_ViewEdit.Rows.Insert (selectedRowIndex -. 1, the newRow);
dataGridView_Task_ViewEdit.ClearSelection ();
// Select the selected row
dataGridView_Task_ViewEdit.Rows [selectedRowIndex-1] .Selected = true;
}
}
}
}
Note:
here is a row up without binding the source of the data source. The additional row passes through the newly added method. Finished.
At the moment, the dataSource of the dataGridView is empty.
The method for obtaining the selected row is used here:
private int GetSelectedRowIndex (DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
return 0;
}
foreach (DataGridViewRow row in dgv.Rows)
{
if (row.Selected)
{
return row.Index;
}
}
return 0 ;
}
action
down one
Private void TaskViewEditHelper_OnDownStep (SENDER Object, EventArgs E)
{
IF (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show ( "please pick line , Click the first column to select the row ");
}
else
{
if (this.dataGridView_Task_ViewEdit.SelectedRows [0] .Index> = this.dataGridView_Task_ViewEdit.Rows.Count-1)
{
XtraMessageBox.Show ("This line is already at the bottom and cannot be moved down!");
}
else
{
int selectedRowIndex = GetSelectedRowIndex (this.dataGridView_Task_ViewEdit);
if (selectedRowIndex <dataGridView_Task_ViewEdit.Rows.Count-1)
{
// copy the selected row
DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows [selectedRowIndex];
// delete the selected row
dataGridViewve ( ow. dataGridView_Task_ViewEdit.Rows [selectedRowIndex]);
// Push the imitation row into the next row
selected dataGridView_Task_ViewEdit.Rows.Insert (selectedRowIndex + 1, newRow);
dataGridView_Task_ViewEdit.ClearSelection ();
// Select the selected row
dataGridView_Task_ViewEdit.Rows [selectedRowIndex + 1] .Selected = true;
}
}
}
}