My issue is very simple and it would be easy if I write my program on ASP.NET. But I apply .NET Framework 4.8 and I have no idea how to get the value from that datagridview if the column is hidden.
for (int i2 = 0; i2 < RowsAmount; i2 )
{
int er = Convert.ToInt32(dataGridView1.Rows[i2].Cells["id_contact"].Value.ToString());
MessageBox.Show($"{er}");
// to do smth with that value.
}
The column named as id_contact is hidden as you can guess. What should I do if the error is "object reference does not point to an instance of an object "?
CodePudding user response:
Recommend adding columns to your DataGridView, set DataPropertyName
to the source whatever that may be (perhaps a DataTable or List). Set Visible property to false for in your case id_contact
.
Setup a BindingList
, BindingSource
to your data source
where the BindingSource becomes the data source of the DataGridView. To get an the invisible column access via the BindingList indexed by the BindingSource Position property.
Here the first column IdColumn (defined in the DataGridView designer) is hidden.
namespace DataGridViewSamples
{
public partial class SampleForm : Form
{
public SampleForm() { InitializeComponent(); }
private readonly BindingSource _bindingSource = new BindingSource();
private BindingList<Item> _bindingList;
private void SampleForm_Load(object sender, EventArgs e)
{
List<Item> items = new List<Item>()
{
new Item(){ Id = 1, Process = false, Product = "Phone 2"},
new Item(){ Id = 2, Process = true, Product = "Phone 3"},
new Item(){ Id = 3, Process = false, Product = "Phone 4"},,
};
_bindingList = new BindingList<Item>(items);
_bindingSource.DataSource = _bindingList;
dataGridView1.DataSource = _bindingSource;
}
private void CurrentIdButton_Click(object sender, EventArgs e)
{
if (_bindingSource.Current != null)
{
MessageBox.Show($"{_bindingList[_bindingSource.Position].Id}");
}
}
}
public class Item
{
public int Id { get; set; }
public bool Process { get; set; }
public string Product { get; set; }
}
}
CodePudding user response:
The decision turned out very simple. I have just forgotten about so-called datatype DataRow. With aid of it and the cycle for I could of solve it faster. I've decided to record the data into datagridview in this way:
if (dTable.Rows.Count > 0)
{
DataRow row;
for (int i = 0; i < dTable.Rows.Count; i )
{
row = dTable.Rows[i];
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["id"].Value =
row["id"];
dataGridView1.Rows[i].Cells["Brief_name"].Value =
row["brief_name"];
dataGridView1.Rows[i].Cells["FullName"].Value =
row["full_official_name"];
dataGridView1.Rows[i].Cells["Adress"].Value =
row["ju_adress"];
dataGridView1.Rows[i].Cells["Code"].Value =
row["EDRPOU"];
dataGridView1.Rows[i].Cells["Branch"].Value =
row["branch_economy"];
dataGridView1.Rows[i].Cells["id_contact"].Value =
row["id_contact_person"];
RowsAmount ;
}
}
Some of these columns are invisible and the previous command int er = Convert.ToInt32(dataGridView1.Rows[i2].Cells["id_contact"].Value.ToString()); works out.
Just I haven't willed to apply BindingSource, because the essential part of my program works without it. But anyway thank you for attention.