2017-01-04 2 views
0

Ceci est probablement facile pour les experts, mais j'essaie de trouver comment ajuster les colonnes d'une grille de données dans une application Windows en utilisant C# dans Visual Studio 2015Redimensionnement automatique des colonnes dans une grille de données à partir de SQL en C#

Mon code est ci-dessous. J'ai lu des commandes comme AutoResize mais je ne peux pas comprendre comment et où le mettre dans mon code. Tout ce que je viens d'essayer arrive avec des erreurs de syntaxe ou il n'y a pas la possibilité de redimensionner:

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 
    } 


} 
} 

J'ai besoin des colonnes pour adapter le texte ou au moins remplir l'espace gris.

Tous les conseils seraient utiles.

Merci

+1

Vous voudrez peut-être supprimer tout ce qui a trait à SQL à partir de votre exemple de code, pour le rendre concis. En ce qui concerne vos problèmes, peu importe d'où proviennent les données. – uncoder

+0

** [Méthode DataGridView.AutoResizeColumns] (https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autoresizecolumns (v = vs.110) .aspx) ** La saisie dans le méthode (aka "commande") et en appuyant sur F1 vous dira tout ce que vous devez savoir – Plutonix

+0

Je vois que vous avez remarqué que je ne suis pas tout cela avec la terminologie de la programmation. Laisse-moi tranquille. J'essaie –

Répondre

1

Vous pouvez le faire comme ça. Cela rendra les colonnes assez large pour le contenu peut tenir dans les:

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(); 
} 

Vous pouvez également passer un paramètre à la AutoResizeColumns. Le paramètre accepté est une énumération de type DataGridViewAutoSizeColumnsMode. Cela vous donnera un contrôle encore plus fin sur le redimensionnement automatique. Voici comment faire:

// This is the default so same as dgvIssues.AutoResizeColumns(); 
dgvIssues.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 

Voici les différentes options que vous avez pour le paramètre:

 // 
     // 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 

Voici une autre façon où vous pouvez le dire plus précisément comment vous voulez que chaque colonne:

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 
    } 
} 
+0

Merci beaucoup. Votre première suggestion a parfaitement fonctionné. Il est bon de voir quelqu'un aider proactivement plutôt que de simplement prendre la pisse parce que j'ai un peu mal la terminologie –

+0

Pas de soucis. Voir mon édition pour plus d'informations si vous en avez besoin. – CodingYoshi