win表单中的数据网格视图不会根据其内容进行调整

时间:2012-02-29 14:24:12

标签: windows winforms windows-forms-designer

我正在尝试在Windows窗体中调整数据网格视图的大小。我的表单上有两个数据网格视图,两个都是从数据库中提供的。是否有任何可以调整左侧数据网格视图的大小,以便它随内容长度和宽度增长,数据网格视图随内容缩小。

以下是我的代码

private void show_Click(object sender, EventArgs e)
    {

        int rowIndex = Convert.ToInt32(productGridView.SelectedRows[0].Cells[2].Value);
        //IEnumerable<Supplier> supplierQuery = from supplier in newNorthWindContext.Suppliers
        //                                      where supplier.SupplierID == rowIndex
        //                                      select new {} supplier;
        IEnumerable<Supplier> supplierQuery = newNorthWindContext.Suppliers.Where(supliers => supliers.SupplierID == rowIndex);

        supplierDataGridView.DataSource = ((ObjectQuery)supplierQuery).Execute(MergeOption.AppendOnly);
        supplierDataGridView.Columns["SupplierID"].Visible = false;
        supplierDataGridView.Columns["ContactTitle"].Visible = false;
        supplierDataGridView.Columns["Address"].Visible = false;
        supplierDataGridView.Columns["City"].Visible = false;
        supplierDataGridView.Columns["Region"].Visible = false;
        supplierDataGridView.Columns["PostalCode"].Visible = false;
        supplierDataGridView.Columns["ContactTitle"].Visible = false;
        supplierDataGridView.Columns["Address"].Visible = false;

        //supplierDataGridView.AutoResizeColumns();
        supplierDataGridView.AutoSizeColumnsMode =
   DataGridViewAutoSizeColumnsMode.AllCells;

    }
    private void categoryDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        Category category = (Category)this.categoryDropDown.SelectedItem;

        try
        {
            productGridView.DataSource = category.Products;
            productGridView.Columns["SupplierID"].Visible = false;
            productGridView.Columns["CategoryID"].Visible = false;
            productGridView.Columns["Category"].Visible = false;
            productGridView.Columns["Order_Details"].Visible = false;
            productGridView.Columns["Supplier"].Visible = false;
            productGridView.AllowUserToDeleteRows = true;
            productGridView.AutoResizeColumns();
            productGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }



    private void ProductDetail_Load(object sender, EventArgs e)
    {

        newNorthWindContext = new NorthwindEntities();
        productGridView.AutoSizeColumnsMode =
    DataGridViewAutoSizeColumnsMode.AllCells;
        IEnumerable<Category> categoryQuery = from category in newNorthWindContext.Categories.Include("Products")
                                              orderby category.CategoryName
                                              select category;


        try
        {

            this.categoryDropDown.DataSource = ((ObjectQuery)categoryQuery).Execute(MergeOption.AppendOnly);
            this.categoryDropDown.DisplayMember = "categoryName";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

enter image description here

2 个答案:

答案 0 :(得分:2)

这解决了这个问题。

supplierDataGridView.Width = supplierDataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+4;

答案 1 :(得分:0)

在show_Click结束时你应该有类似

的东西
supplierDataGridView.Width = supplierDataGridView.Columns.Sum(x => x.Width) + supplierDataGridView.RowHeadersWidth + 2;

supplierDataGridView.Height = supplierDataGridView.GetRowDisplayRectangle(supplierDataGridView.NewRowIndex, true).Bottom + supplierDataGridView.GetRowDisplayRectangle(supplierDataGridView.NewRowIndex, false).Height;

我希望这有帮助!