I have a quick question regarding getting a specific value from a data grid, say I've got 4 columns, id surname, firstname and phone number. I want to get the ID From the selected row that the user has currently selected ? how would I go around this, I've tried several attempts but it's just returning the whole row rather than the single ID.
Thanks again
Rolls
((class)yourGrid.SelectedItem).ID
In "class" put class of your object stored in grid.
If you know the index of the id column you can get DataGridCell using code at:
Programmatically edit a datagrid cell in wpf
Then you can get ID by getting DataSource for DataGridCell.
Related
I am trying to retrieve a value from a column within a data grid view (gv_Quals). I can't seem to find the correct answer online and this is my attempt. The Unit ID is the second column within the Sql Database. The row index is passed down.
String txtUnitID =gv_Quals.Rows[rowIndex].Cells[1].ToString();
When I try and run this code, txtUnitID will always return this
txtUnitID = "System.Web.UI.WebControls.DataControlFieldCellstring"
when it should be returning 5 as that is the UnitID. I'm I doing it wrong or have I forgot something. Sorry if I seem vague, thanks in advance!
If there is DataBound column in your grid than you can get value using
gv_Quals.Rows[rowIndex].Cells[1].Text
Or if its any control then you have to first cast that control and then you can get value from that control.
I am wondering of how to pass data, that's one row from Grid View1 to Grid View2 in the same Web form!I created two tables, the first one contains four columns and contains records: Id, name, address and age.
I also created the second form which also contains four columns: Id, name, address and age, but left empty for purpose of transmitting data from the first table.
I wonder if it is possible if to select one row from Grid view1 and then click a button which enables to pass that selected row to Grid View2 !? One row each a time added after hitting the send button to the second grid view and guarantees that data added to the second table!
Any ideas of how to do that would be much appreciated.
I'll give you an overview of some steps you might want to take
I assume you use a DataSet or a DataTable where your records are stored in.
use the eventHandler of the gridview, like CellClick, most of them have a e.RowIndex value, which you can use to get the index of the row that is selected
Like this:
private void dg1_CellClick(object sender, DataGridViewCellEventArgs e)
{
selectedIndex = e.RowIndex;
}
use that index to get the id from that record, so you know what data you are talking about
like this:
int id = Convert.ToInt32(dataset1.Tables[0].Rows[selectedIndex][0]);
then just find the row by id or use the knowledge above to combine this
DataRow row = dataset1.Tables[0].Rows.Find(Id);
dg2.Rows.Add(row);
it's better to use datasets and give the datagridview a datasource to that dataset, not sure what your intentions are
you could also get the id from the row you selected, based on that you call another sqlCommand to get that current row, a good way if you have more data you want to load from the database that rely on that id
I have a data loaded from database into dataGridView, with effect looking like this:
(in final version I would prefer to hide the PatientID column)
What I'm trying to do, is return value of PatientID when user clicks ANYWHERE in the corresponding row (i.e. user clicks "Doe" and value returned is "2"). Could anyone give me a hint how to do this? I don't think there is valueMember property... I was trying Rowindex but that returns value of number of row counting from the top(D'uh?!)
Also, is there a way for user to highlight whole row when clicking on the single cell?
EDIT: Oh God, I've spent few hours late at night to find this, In the morning I gave up and posted here... just to find answer 5 minutes later:
string test = dataGridView1.Rows[e.RowIndex].Cells["PatientID"].FormattedValue.ToString();
Still, that leaves my second question about highlighting whole row.
If the datagridview is bound to some data source (DataView), you can use DataBoundItem property, for example
DGV.CurrentRow.DataBoundItem["PatientID"]
or
DGV.SelectedRows[0].DataBoundItem["PatientID"]
or
DGVUnderlyingBindingSource.Current["PatientID"]
If the DataGridView is bound to a strongly typed data source (e.g. BindingList) then you can use the above like this:
((PatientType)DGV.CurrentRow.DataBoundItem).PatientID
or
((PatientType)DGV.SelectedRows[0].DataBoundItem).PatientID
or
((PatientType)PatientTypeBindingSource.Current).PatientID
About the second part of the question, set the DataGridView's SelectionMode property to FullRowSelect
EDIT
You can't use the solution from your edit if you hide that column. In order to access the value by using .Cells[idx].FormattedValue that value must be visible. But you can use this one even if you hide the column.
For the second question, set selection mode to FullRowSelect and the complete row will be highlighted when the user clicks on it. You can set this attribute in the designer or code:
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
I think it would be easier for you to understand my problem if I explain the scenario a bit.
What I am doing is developing a system to a doctor using asp.net c#. For the creation of the prescription, I add a check box list for the list of medicines and submit button below it. When the submit button clicked selected check box list items added to the sql server database and show a data grid with the selected medicines in the same page.
In the data grid there are two columns for medicine name and dosage.Medicine name is taken from the database.I need to give the user the ability to enter the dosage through that grid view. I need to use update command because the record is already there in the sql server.But empty value in the dosage column.I tried to use update command in the data source control. But I can't figure out how to give the WHERE clause because I can't specify the database row which the updating record relate to.
I would be glad to hear some help from you. All the comments are welcome.
You need to set a parameter(s), please check this example to understand how it works. I guess, that you've added in the database some column that is unique identifier.. some autoincrement int or guid.
In the search form by display button records are displayed from database. but i wnt to
change the header text of data grid column like member_id as memberID.
how can i do it?
as i m trying datagridview.column[1].headerText="memberID" it shows error of indexes of collection so what code i will use to change the header text?
try grid.Columns[0].HeaderText = "memberID". Your column indices may be running out of bound.