[雙擊從可用移動到選中][1]
我有 2 個資料網格視圖,我想將用戶單擊的特定單元格從一個網格移動到另一個網格。
我試過這個
private void availableFacilList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var selectedMove = availableFacilList.Rows[e.RowIndex].Cells[0].Value.ToString();
DataGridViewRow row = (DataGridViewRow)selectedFacilList.Rows[0].Clone();
row.Cells[0].Value = selectedMove;
selectedFacilList.Rows.Add(row);
}
附上外觀的圖片 [1]:https ://i.stack.imgur.com/qjWiX.png
uj5u.com熱心網友回復:
如評論中所述,如果第二個網格至少有一列,則發布的代碼似乎可以作業。我相信這就是為什么在代碼行上拋出索引超出范圍例外的原因......
DataGridViewRow row = (DataGridViewRow)selectedFacilList.Rows[0].Clone();
由于 ... selectedFacilList.Rows[0]... 行零 (0) 不存在。
對兩個網格都使用 aDataSource可能會使您的事情變得更容易。在這種情況下,如果兩個網格使用DataTables相同架構的兩個不同的網格,那么您需要做的就是將選定的行從一個DataTable移動到另一個,網格將自動更新。
首先,我們要創建兩 (2) 個具有相同架構的資料表。它將只有一string列,我們將使用這些表將選定的行從一個表移動到另一個表,反之亦然。
創建這些表格并用左側網格的一些測驗資料填充其中的表格可能看起來與您的圖片顯示的相似,只是右側的網格至少有一列。
DataTable AvailableDT;
DataTable SelectedDT;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AvailableDT = GetEmptyDT();
SelectedDT = GetEmptyDT();
FillTableWithData(AvailableDT);
availableFacilGrid.DataSource = AvailableDT;
selectedFacilGrid.DataSource = SelectedDT;
}
private DataTable GetEmptyDT() {
DataTable dt = new DataTable();
dt.Columns.Add("Group", typeof(string));
return dt;
}
private void FillTableWithData(DataTable dt) {
dt.Rows.Add("Max Group of Hospitals");
dt.Rows.Add("Fortis Group of Hospitals");
dt.Rows.Add("Manipal Group of Hospitals");
dt.Rows.Add("Appolo Group of Hospitals");
}
接下來我們需要訂閱每個網格的CellDoubleClick事件來前后移動行。為了簡化事情,不是撰寫代碼將行從左向右移動,而是撰寫更多代碼將行從右向左移動……我們撰寫了一種可以同時適應這兩種移動的方法。
In this method, we will need to know which “Grid” is the “source” grid so we can get the current selected cell from it. From that passed in grid we can get its DataSource DataTable so we can remove the selected row. The only other thing we need is the destination grids data source to add the row to. These two items are passed into the MoveRow method. This should simplify moving the rows either way and may look something like below…
private void MoveRow(DataGridView sourceGrid, DataTable destDT) {
int sourceRowIndex = sourceGrid.CurrentRow.Index;
if (sourceRowIndex >= 0 && !sourceGrid.Rows[sourceRowIndex].IsNewRow) {
DataRowView selectedRow = (DataRowView)sourceGrid.Rows[sourceRowIndex].DataBoundItem;
destDT.ImportRow(selectedRow.Row);
((DataTable)sourceGrid.DataSource).Rows.Remove(selectedRow.Row);
}
}
The above code is straight forward… first we get the row index of the source grid’s CurrentRow. This is the row we want to move to the destination grid’s data source. Some bounds checking is made in addition to checking if the row is the grids NewRow in which case we do not want to move that row. If the checks are passed, then we grab the row from the grid with the line of code…
DataRowView selectedRow = (DataRowView)sourceGrid.Rows[sourceRowIndex].DataBoundItem;
This will get the row from the grids DataSource. You may note that when the code sets the sourceRowIndex variable with … int sourceRowIndex = sourceGrid.CurrentRow.Index; … this is the row index in relation to the GRID ROWS… and you may be tempted to get the row from the grids DataSource using THAT index with something like…
DataRowView selectedRow = sourceDT.Rows[sourceRowIndex];
This may work and it may not work. If the grid is NOT sorted or filtered, then there is a good chance that there IS a one-to-one correspondence to the row index in the gird and the row index in the grids underlying DataTable. However, if the grid is sorted or filtered, then this one-to-one correspondence is lost. In other words, the row at grid row index 3, may not necessarily be at row index 3 in the underlying data sources DataTable.
To ensure we get the “right” row from the grids data source, each grid Row has a DataBoundItem property that will give us the DataRowView of the corresponding DataRow in the DataTable. The code above shows this and it makes it much easier to add and remove that row from the table.
After we have the DataRowView of the source DataTable it is trivial to “import” the row from the sourceDT table to the destination destDT table. Then remove the row from the sourceDT table. Note, you will get an error if you attempt to simply “Add” the row from the source to the destination table. In other words… instead of using the import code like…
destDT.ImportRow(selectedRow.Row);
We simply added the row like…
destDT.Rows.Add(selectedRow.Row);
該錯誤將抱怨您無法將行添加到已經屬于另一個表的表中。即使您在嘗試將其添加到目標表之前從源表中“洗掉”該行……您仍然會收到錯誤訊息。我打賭該行仍然存在,但只是標記為洗掉。
幸運的是,使用DataTables ImportRow避免了這種情況并幫助我們“復制”行。因此,鑒于上述MoveRow方法,它應該簡化兩個網格CellDoubleClick事件中的代碼,并且可能看起來像……
private void availableFacilGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
MoveRow(availableFacilGrid, SelectedDT);
}
private void selectedFacilGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
MoveRow(selectedFacilGrid, AvailableDT);
}
我希望這會有所幫助并且有意義。
uj5u.com熱心網友回復:
這是您的問題的粗略實作。使用代碼作為基礎,因為它包含基本邏輯并將其調整為您的變數和事件處理程式。快樂編碼!
DataTable DTAvailable, DTSelected;
internal void SetupGrid(Grid _grid, string _columnName)
{ //Add the columns for the _grid object with the name _columnName}
internal void DoGridsLoad()
{
DTSelected = LoadData();
SetupGrid(Grid1, "Selected");
Grid1.DataSource = DTAvailable;
SetupGrid(Grid2, "Available");
DTSelected = new DataTable();
Grid2.DataSource = DTSelected;
}
internal void MoveToSelected(object sender, EventArgs e) //BTN Move to Selected
{
DataRow toMove = DTAvailable[Grid1.CurrentRowIndex]; //locate Row
DTSelected.Rows.Add(toMove);
DTAvailable.Rows.Remove(toMove);
}
internal void MoveToAvailable(object sender, EventArgs e) //BTN Move to Available
{
DataRow toMove = DTSelected[Grid2.CurrentRowIndex]; //locate Row
DTAvailable.Rows.Add(toMove);
DTSelected.Rows.Remove(toMove);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424203.html
