简体   繁体   English

从列表中获取特定属性

[英]Getting specific properties from a list

i am facing a problem 我面临一个问题

i have a class 我有一堂课

public class StudentDetails
{    
    int S_Detail_ID;
    string address;
    string email;    
}

containg get set method and iam calling a function which is collecting the List<StudentDetails> and showing it in datagridview the problem is that list is returning all properties of the class, but i only want address and email not s_detail_id Containg get set方法和IAM调用一个函数,该函数正在收集List<StudentDetails>并将其显示在datagridview中,问题是列表返回了该类的所有属性,但是我只希望地址和电子邮件不s_detail_id

here is code for the function 这是该功能的代码

private void btnAddNewRowInGrid_Click(object sender, EventArgs e) 
{

    List<StudentDetails> lstStudentDetails = GetStudentDetails();
    lststudentDetails.Add(new StudentDetails()); //what to do here, studentDetails is returning all properties but i want only addres and email
    dataGridView1.DataSource = lstStudentDetails;
} 

and GetStudent jusst count rows in datagridview and add a new row 和GetStudent要求对datagridview中的行进行计数并添加新行

private List<StudentDetails> GetStudentDetails()
{
    lstStudentDetails = new List<StudentDetails>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {

        lstStudentDetails.Add(row.DataBoundItem as StudentDetails);
    }
    return lstStudentDetails;
}
  1. Select DataGridView in designer and click on little triangle at top right corner of control 在设计器中选择DataGridView,然后单击控件右上角的小三角形
  2. Click Edit Columns menu item 单击“ 编辑列”菜单项
  3. Manually add columns for address and email properties (type in property name in DataPropertyName item of column properties) 手动添加地址和电子邮件属性的列(在列属性的DataPropertyName项目中键入属性名称)
  4. Disable columns auto-generation 禁用列自动生成

在此处输入图片说明

You cannot turn off columns auto-generation from designer - its possible only from code: 您不能关闭来自Designer的列自动生成-仅可以通过代码关闭:

dataGridView1.AutoGenerateColumns = false;

After that your code will work. 之后,您的代码将起作用。

您可以通过将可见性设置为false来尝试隐藏不需要的列。

dataGridView1.Columns["S_Detail_ID"].Visible = false;

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

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