The code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ImageCoordinateMapper
{
public partial class CsvReader : UserControl
{
private DataGridView dataGridView;
private Button buttonLoadCsv;
public CsvReader()
{
InitializeComponent();
}
private void buttonLoadCsv_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "CSV files (*.csv)|*.csv";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
LoadCsvFile(filePath);
this.Size = new System.Drawing.Size(1920, 1080);
}
}
public void LoadCsvFile(string filePath)
{
DataTable dataTable = new DataTable();
try
{
using (StreamReader sr = new StreamReader(filePath))
{
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
dataTable.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
DataRow dr = dataTable.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dataTable.Rows.Add(dr);
}
}
dataGridView.DataSource = dataTable;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void CsvReader_Load(object sender, EventArgs e)
{
}
}
}
and then i drag it from the toolbox in the form1 designer then click the button and loading a csv file:
the first problem is that when i try to resize the control in the form1 designer it’s not resizing the whole control the inside gray part not changing. the button is resizing dynamic fine but not the gray area part inside the control.
i also tried in the code to resize the control but it’s not effecting it in any way with this line:
this.Size = new System.Drawing.Size(1920, 1080);
no matter where i drag the control in the form1 designer it will always will be at the bottom no matter if i drag it to the top.
the main goal is to automatically resize the control depending on the content so i can see the whole columns and rows contents.