1
votes

I have a Datagridview, and a field from the database. I want save this field to row tag in gridview, but after saving to the tag with a Foreach loop, in another function I see it returns null value. How do I save this field? Thank you

private void UC_DKHoc_Load(object sender, EventArgs e)
{
    var dsLop = LopDangKyServices.LayDanhSachLopDangKy();
    var DatasourceGV = from lop in dsLop
                select new
                {
                    lop.MaLopDangKy,
                    lop.TenLopDangKy,
                    lop.CLB1.TenCLB,
                    lop.NamHoc,
                    lop.HocPhi,
                    lop.LichHoc
                };
    advancedDataGridView.DataSource = DatasourceGV.ToList(); // Display to gridview
    int i = 0;
    foreach (DataGridViewRow row in advancedDataGridView.Rows)
    {
        row.Tag = dsLop[i++].CLB; //save complete
    }
    advancedDataGridView.DisableFilter(STT);
}

In The function I see null values.

private void simpleButton1_Click(object sender, EventArgs e)
{  
    MessageBox.Show(advancedDataGridView.Rows[1].Tag.ToString()); //Null value
}
1
About DisableFilter is the method rebind the datasource and you lost the tags on it. - Oleg
You should add the CLB value in the select new and define columns of the datagridview that are shown. - user12031933
try with cell index in row. And check by commenting DisableFilter statement row.Cells[0].Value = dsLop[i++].CLB; In above statement 0 will the cell number of the Tag in row. - Sibgath
I don't want show this field in gridview, I just want save it. - DuckFterminal
I mean to something like this: social.msdn.microsoft.com/Forums/windows/en-US/… - Oleg

1 Answers

1
votes

You can use either of the following options:

  • Include all the properties which you need in the list (including CLB). Then after assigning the list as DataSource of the DataGridView, hide the corresponding column:

    advancedDataGridView.DataSource = list;
    advancedDataGridView.Columns["CLB"].Visible = false;
    
  • Create a model class containing all the properties which you need, including CLB. Then just decorate CLB with [Browsable(false)] attribute. (You need to select the result by creating instance of the model class, rather than anonymous type.)

    [Browsable(false)]
    public int CLB { get; set;}
    
  • Set AutoGenerateColumns of DataGridView to false and add desired columns to DataGridView. Make sure you set Name, DataPropertyName and HeaderText. Then set DataSource of DataGridView to the list which contains all properties, including CLB:

    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { 
        Name = "MaLopDangKy", 
        DataPropertyName = "MaLopDangKy", 
        HeaderText = "MaLopDangKy" });
    
    //Add rest of columns ...
    
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = list;
    

    You can decide to add CLB as an invisible column of DataGridView. Even if you didn't add it as a column, you can access to the DataBoundItem property of the DataGridViewRow which points to the list member instance which is displaying the the row. Cast it to the list member type and use its CLB property.