i have dgv and i bound it like this dgviewOrders.DataSource = Orders but i would like to display objects without thier ID and when i select a row i want to get the whole object because this is what my teacher asked me to do but i couldnt do it
//i tried this code:
var _bind = from a in Orders
select new OrderItem
{
ItemName = a.ItemName,
Amount = a.Amount,
Comment = a.Comment,
Status = a.Status,
TableNumber = a.TableNumber,
Ordertime = a.Ordertime,
Id = a.Id
};
//dgviewOrders.DataSource = _bind.ToList();
dgviewOrders.DataSource = Orders;
and this is how i get the selected object : OrderItem currentObject = (OrderItem)row.DataBoundItem; but when i comment the Id in the orders the object is empty
DataGridView has AutogenerateColumns
set to true
by default. This means it will fill the dgViewOrders.Columns
collection with DataColumn objects when bound to a data source
You have two choices for not displaying a column:
set autogeneratecolumns
to false, and fill the column collection yourself manually, and don't include the column you do not want to see
leave autogeneratecolumns
as true, and after you have bound your object and the columns have been generated, hide the one you don't want (`dgViewOrders.Columns["Id"].Visible = false;)
This MSDN article has some more info, but is essentially that line of code above
when i select a row i want to get the whole object
To retrieve the currently selected data bound item for a row you have a couple of choices:
datagridview.CurrentRow
This returns you a reference to the datagridviewrow that contains the current cell. The current cell is the one with the faint outline around it that would receive the keyboard focus if you started typing. It can be moved with the cursor and is NOT necessarily a selected row
datagridview.SelectedRows
Returns you a collection of datagridviewrows that have been selected (turned blue) with the mouse, or by holding shift and moving the current row with the cursor keys. If your DGV is in single row select mode the collection will have only one row. Your DGV's selectionmode
property MUST be fullrowselect
or rowheaderselect
- if it is set to anything else then the collection will not be populated
Whichever one of the above you choose, you will get one or more DataGridViewRow
objects. They will have a .DataBoundItem
property that, if your datasource is a DataTable, will probably be a DataRowView. That in turn will have a .Row
property that is your datarow. This will either be a strongly typed (YourXyzNameHereDataRow) or a weakly typed (DataRow)
Hence if youre using datatables, your code will look something like:
'generic datatable
Dim dr as DataRow = DirectCast(DirectCast(dgViewOrders.CurrentRow.DataBoundItem, DataRowView).Row, DataRow)
'strongly typed datatable
Dim dr as OrdersDataRow = DirectCast(DirectCast(dgViewOrders.CurrentRow.DataBoundItem, DataRowView).Row, OrdersDataRow)
If youre not using datatables then it'll be something else in DataBoundItem; use the debugger to inspect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.