简体   繁体   中英

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

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

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
  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)
  4. Disable columns auto-generation

在此处输入图片说明

You cannot turn off columns auto-generation from designer - its possible only from code:

dataGridView1.AutoGenerateColumns = false;

After that your code will work.

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

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

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.

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