This is probably easy for the experts among you but I am trying to find out how to adjust the columns of a data grid in a windows application using C# in Visual Studio 2015.
My code is below. I have read about commands like AutoResize but I can not figure out how and where to put it in my code. Everything I try just comes up with syntax errors or there isn't the option to resize:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Main : Form
{
int BugID = 0;
public Main()
{
InitializeComponent();
}
private void txtUser_TextChanged(object sender, EventArgs e)
{
}
Reset();
FillDataGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There is an error. Please review");
}
finally
{
sqlCon.Close(); // close the connection
}
}
void FillDataGridView() // The below is to display the search results in the Data Grid View box using the stored procedure for search results
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("BugViewOrSearch", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@BugIssue", txtSearch.Text.Trim());
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
dgvIssues.DataSource = dtbl;
sqlCon.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
FillDataGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There is an error. Please review");
}
}
private void dgvIssues_DoubleClick(object sender, EventArgs e)
{
if(dgvIssues.CurrentRow.Index != -1) // For updating an issue when double clicking on a row/issue
{
BugID = Convert.ToInt32(dgvIssues.CurrentRow.Cells[0].Value.ToString());
txtUser.Text = dgvIssues.CurrentRow.Cells[1].Value.ToString();
txtSubject.Text = dgvIssues.CurrentRow.Cells[2].Value.ToString();
txtDescription.Text = dgvIssues.CurrentRow.Cells[3].Value.ToString();
btnCreate.Text = "Update Issue"; // Changes the button from 'Create Issue' to 'Update Issue'
btnDelete.Enabled = true;
}
}
void Reset() // To reset all field of the form
{
txtUser.Text = txtSubject.Text = txtDescription.Text = "";
btnCreate.Text = "Create Issue";
BugID = 0;
btnDelete.Enabled = false;
}
private void btnRefresh_Click(object sender, EventArgs e)
{
Reset(); // Calls the reset function above
}
private void Main_Load(object sender, EventArgs e)
{
Reset();
FillDataGridView();
// To show all bugs/issues in the database
}
}
}
I need the columns to fit the text or at least fill the grey space.
Any tips would be useful.
Thank you
You can do it like this. This will make the columns wide enough so the contents can fit in them:
You can also pass a parameter to the
AutoResizeColumns
. The parameter it accepts is an enum of typeDataGridViewAutoSizeColumnsMode
. This will give you even finer control on the auto resize. Here is how to do that:Here are the different options you have for the parameter:
Here is another way where you can tell it specifically how wide you want each column: