I am developing an smart device application on windows 6 smart device so DataGridView is not available. I want to get selected row cell values and hide some columns. Is it possible in DataGrid?
Asked
Active
Viewed 1,262 times
0
-
Please post some code on what you have tried already. – Harry Apr 06 '16 at 07:12
3 Answers
0
For a single cell, you can use:
var cellInfo = dataGrid.SelectedCells[0];
This returns a DataGridCellInfo object containing the information of the selected cell.
To hide columns, you have to set their Visibility property to Visibility.Collapsed.
dataGrid.Columns[index].Visibility = Visibility.Collapsed;
Hope this helps!
But next time provide some code please.

SLNC
- 11
- 2
-
First both your codes are not working as i dont find properties like dataGrid.SelectedCells and datagrid .Columns – Mohammad Rashid Apr 06 '16 at 07:51
-
Strange. Both codes work for me. Are you sure you are using a DataGrid object? – SLNC Apr 06 '16 at 08:11
0
You can simply get all selected values from your DataGrid with this code, written in your window's xaml.cs
file:
var selectedItems = this.dataGrid1.SelectedItems;
Also, if you want to select always entire row, you have to change property of your dataGrid
:
this.dataGrid1.SelectionUnit = DataGridSelectionUnit.FullRow;
And for hiding the columns you should change column's Visibility property to Collapsed:
this.dataGrid1.Columns[0].Visibility = Visibility.Collapsed;

Alexey Koptyaev
- 187
- 2
- 16
0
You can use the following CellClick Event of DataGridView to select a row in your datagrid and to show the values of the columns in the specified textboxes.
private void dgvUserList_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex!=-1)
{
id = Convert.ToInt32(dgvUserList.Rows[e.RowIndex].Cells["ID"].Value.ToString());
SqlConnection _sqlconnection = new SqlConnection(Database.ConnectionString);
if (_sqlconnection.State == ConnectionState.Closed || _sqlconnection.State == ConnectionState.Broken)
{
_sqlconnection.Close();
_sqlconnection.Open();
}
SqlCommand _sqlcommand = new SqlCommand("SELECT * FROM Users WHERE ID='" + id + "'");
_sqlcommand.Connection = _sqlconnection;
_sqlcommand.CommandType = CommandType.Text;
SqlDataAdapter _sqldataadapter = new SqlDataAdapter(_sqlcommand);
DataTable _datatable = new DataTable();
_sqldataadapter.Fill(_datatable);
foreach (DataRow _datarow in _datatable.Rows)
{
txtUsername.Text = _datarow["Username"].ToString();
txtPassword.Text = _datarow["Password"].ToString();
}
}
}

Asif Patankar
- 97
- 4
- 15
-
I am not using "DataGridVIew" I can only use DataGrid.. Well i found solution of one of my problem.. datagrid[datagrid.CurrentRowIndex,columnindex].ToString(); – Mohammad Rashid Apr 06 '16 at 08:53