简体   繁体   English

如何在C#中获取DataGrid的选定行值并从DataGrid中隐藏列?

[英]How can I get selected row values of a DataGrid in C# and Hide a column from DataGrid?

I am developing an smart device application on windows 6 smart device so DataGridView is not available. 我正在Windows 6智能设备上开发智能设备应用程序,因此DataGridView不可用。 I want to get selected row cell values and hide some columns. 我想获取选定的行单元格值并隐藏一些列。 Is it possible in DataGrid? 在DataGrid中可以吗?

For a single cell, you can use: 对于单个单元格,可以使用:

var cellInfo = dataGrid.SelectedCells[0];

This returns a DataGridCellInfo object containing the information of the selected cell. 这将返回一个DataGridCellInfo对象,其中包含所选单元格的信息。

To hide columns, you have to set their Visibility property to Visibility.Collapsed. 若要隐藏列,您必须将其Visibility属性设置为Visibility.Collapsed。

dataGrid.Columns[index].Visibility = Visibility.Collapsed;

Hope this helps! 希望这可以帮助!

But next time provide some code please. 但是下次请提供一些代码。

You can simply get all selected values from your DataGrid with this code, written in your window's xaml.cs file: 您可以使用以下代码简单地从DataGrid中获取所有选定的值,这些代码写在窗口的xaml.cs文件中:

 var selectedItems = this.dataGrid1.SelectedItems;

Also, if you want to select always entire row, you have to change property of your dataGrid : 另外,如果要始终选择整行,则必须更改dataGrid属性:

this.dataGrid1.SelectionUnit = DataGridSelectionUnit.FullRow;

And for hiding the columns you should change column's Visibility property to Collapsed: 为了隐藏列,您应该将列的Visibility属性更改为Collapsed:

this.dataGrid1.Columns[0].Visibility = Visibility.Collapsed;

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. 您可以使用以下DataGridView的CellClick事件在数据网格中选择一行,并在指定的文本框中显示列的值。

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();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM