c# - 调用MySql Table时自定义DataGridview宽度

标签 c# mysql datagridview datagridviewcolumn

我正在尝试制作一个程序,列出所选文件夹的所有子文件夹的名称,它一切正常,但我似乎无法为我的 datagridview 制作自定义宽度,我已经寻找答案几个小时了,但它们大多不起作用,我尝试过:

Gata.columns[0].width = 100; 或类似的东西,但它们不起作用。

这也不起作用:MSDN - DataGridViewColumn.Width Property

它们似乎主要用于未绑定(bind)的网格,我不知道我试图将我的 MySql 表链接到一个网格,但又失败了。这几乎是我正在编写的第二个程序,所以请原谅我的笨拙!

我希望我的表格看起来像 picture here.

网格属性上的自动调整大小和填充语句不起作用。 我检查了很多有关堆栈溢出的答案,但没有一个回答这个问题。预先感谢您的帮助!

这是我使用的代码:

            try
            {
                String sqlcon = "datasource=localhost;port=3306;username=anime;password=anime";
                MySqlConnection myanimedb0con = new MySqlConnection(sqlcon);
                MySqlDataAdapter myanimedb0ada = new MySqlDataAdapter();
                MySqlCommand myanimedb0cmd = new MySqlCommand("insert into anime.anime0list ( Anime_Name , Anime_Root ) values ( '" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(dir.Name) + "' , '" + dir.Parent + "' );", myanimedb0con);
                MySqlCommandBuilder myanimedb0cb = new MySqlCommandBuilder(myanimedb0ada);
                myanimedb0ada.SelectCommand = myanimedb0cmd;
                DataTable Gate = new DataTable();
                myanimedb0ada.Fill(Gate);
                BindingSource b0Gate = new BindingSource();
                b0Gate.DataSource = Gate;
                this.Gate.DataSource = b0Gate;
                myanimedb0ada.Update(Gate);

                // Updating the table after adding an item

                MySqlCommand myanimedb0cmd2 = new MySqlCommand("select * from anime.anime0list ;", myanimedb0con);

                myanimedb0ada.SelectCommand = myanimedb0cmd2;

                myanimedb0ada.Fill(Gate);
                b0Gate.DataSource = Gate;
                this.Gate.DataSource = b0Gate;
                myanimedb0ada.Update(Gate);

最佳答案

如果您使用默认的 Windows 窗体或 Web 控件,一种方法是一次设置一个列宽,如下所示:

private void Button5_Click(object sender, System.EventArgs e)
{
    DataGridViewColumn column = dataGridView.Columns[0];
    column.Width = 60;
}

如果您愿意,您也可以像这样按列进行操作:

for (int i = 0; i < DataGridView1.Columns.Count; i++)
{
    DataGridView1.Columns[i].Width = 30;
}

或者您可以在事件中操纵它...

dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);   
void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)   
{   
    e.Column.Width = 30;   
}

这必须有效,有保证。如果你来对了,请告诉我?

关于c# - 调用MySql Table时自定义DataGridview宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383449/

相关文章:

c# - 在另一台显示器上工作时保持主显示器全屏显示

php - 从 PHP 表多次插入 MySQL 表

MySQL 性能,当查询使用 order by/group by 时子查询使用临时文件排序

VB.NET Windows 窗体上下文菜单位置

mysql - VB.net从Mysql加载数据到datagridview

c# - 如何排队服务器 ASP.NET 完成的工作

c# - 如何修复 HTTP 错误 500.22 - 内部服务器错误检测到不适用于集成托管管道模式的 ASP.NET 设置

c# - 等待结果而不阻塞线程

PHP MYSQL 检查行数据是否与

c# - 除非重新启动应用程序,否则 DataGridView 不会更新