使用 C# 从 SQL 自动调整数据网格中列的大小
Auto Resize of Columns in a Data Grid from SQL using C#
这对于你们中间的专家来说可能很容易,但我正在尝试找出如何在 Visual Studio 2015 年使用 C# 调整 windows 应用程序中数据网格的列。
我的代码如下。我已经阅读了有关 AutoResize 之类的命令,但我无法弄清楚如何以及将它放入我的代码中的位置。我尝试的一切都只是出现语法错误,或者没有调整大小的选项:
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
}
}
}
我需要列适合文本或至少填充灰色 space。
任何提示都会有用。
谢谢
你可以这样做。这将使列足够宽,以便内容可以容纳在其中:
void FillDataGridView() // The below is to display the search results in the Data Grid View box using the stored procedure for search results
{
// Your code here
// This is after your code
dgvIssues.AutoResizeColumns();
}
您还可以将参数传递给 AutoResizeColumns
。它接受的参数是 DataGridViewAutoSizeColumnsMode
类型的枚举。这将使您更好地控制自动调整大小。以下是如何做到这一点:
// This is the default so same as dgvIssues.AutoResizeColumns();
dgvIssues.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
以下是参数的不同选项:
//
// Summary:
// The column widths do not automatically adjust.
None = 1,
//
// Summary:
// The column widths adjust to fit the contents of the column header cells.
ColumnHeader = 2,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns, excluding
// header cells.
AllCellsExceptHeader = 4,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns, including
// header cells.
AllCells = 6,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns that
// are in rows currently displayed onscreen, excluding header cells.
DisplayedCellsExceptHeader = 8,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns that
// are in rows currently displayed onscreen, including header cells.
DisplayedCells = 10,
//
// Summary:
// The column widths adjust so that the widths of all columns exactly fill the display
// area of the control, requiring horizontal scrolling only to keep column widths
// above the System.Windows.Forms.DataGridViewColumn.MinimumWidth property values.
// Relative column widths are determined by the relative System.Windows.Forms.DataGridViewColumn.FillWeight
// property values.
Fill = 16
这是另一种方式,您可以通过这种方式明确指定每列的宽度:
void FillDataGridView() // The below is to display the search results in the Data Grid View box using the stored procedure for search results
{
// Your code here
// This is after your code
// For individual columns, do it like this:
foreach( var column in dgvIssues.Columns) {
column.Width = 100; // Or whatever you like
}
}
这对于你们中间的专家来说可能很容易,但我正在尝试找出如何在 Visual Studio 2015 年使用 C# 调整 windows 应用程序中数据网格的列。
我的代码如下。我已经阅读了有关 AutoResize 之类的命令,但我无法弄清楚如何以及将它放入我的代码中的位置。我尝试的一切都只是出现语法错误,或者没有调整大小的选项:
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
}
}
}
我需要列适合文本或至少填充灰色 space。
任何提示都会有用。
谢谢
你可以这样做。这将使列足够宽,以便内容可以容纳在其中:
void FillDataGridView() // The below is to display the search results in the Data Grid View box using the stored procedure for search results
{
// Your code here
// This is after your code
dgvIssues.AutoResizeColumns();
}
您还可以将参数传递给 AutoResizeColumns
。它接受的参数是 DataGridViewAutoSizeColumnsMode
类型的枚举。这将使您更好地控制自动调整大小。以下是如何做到这一点:
// This is the default so same as dgvIssues.AutoResizeColumns();
dgvIssues.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
以下是参数的不同选项:
//
// Summary:
// The column widths do not automatically adjust.
None = 1,
//
// Summary:
// The column widths adjust to fit the contents of the column header cells.
ColumnHeader = 2,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns, excluding
// header cells.
AllCellsExceptHeader = 4,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns, including
// header cells.
AllCells = 6,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns that
// are in rows currently displayed onscreen, excluding header cells.
DisplayedCellsExceptHeader = 8,
//
// Summary:
// The column widths adjust to fit the contents of all cells in the columns that
// are in rows currently displayed onscreen, including header cells.
DisplayedCells = 10,
//
// Summary:
// The column widths adjust so that the widths of all columns exactly fill the display
// area of the control, requiring horizontal scrolling only to keep column widths
// above the System.Windows.Forms.DataGridViewColumn.MinimumWidth property values.
// Relative column widths are determined by the relative System.Windows.Forms.DataGridViewColumn.FillWeight
// property values.
Fill = 16
这是另一种方式,您可以通过这种方式明确指定每列的宽度:
void FillDataGridView() // The below is to display the search results in the Data Grid View box using the stored procedure for search results
{
// Your code here
// This is after your code
// For individual columns, do it like this:
foreach( var column in dgvIssues.Columns) {
column.Width = 100; // Or whatever you like
}
}