2016-08-09 24 views
0

我在C#窗口应用程序中使用了一个gridview。其中一个单元格有一长串文本(见下文),但每当它绑定到网格时,单元格将被裁剪,滚动条将显示全文。用鼠标拖动时滚动条也不能平滑滚动。在单元格C#gridview窗口中获取裁剪文本

我尝试了RowSize和ColumnSize`模式的不同组合,但没有运气。

void FillGrid() 
{ 
    DataTable tasktable = new DataTable(); 
    tasktable.Columns.Add("Logged By", typeof(string)); 
    tasktable.Columns.Add("Date", typeof(DateTime)); 
    tasktable.Columns.Add("Notes", typeof(string)); 
    DataRow dr1; 
    //for (int i = 0; i < 100; i++) 
    //{ 
    dr1 = tasktable.NewRow(); 
    dr1[0] = "Sunit Shah"; 
    dr1[1] = System.DateTime.Now; 
    dr1[2] = "Test Note"; 
    tasktable.Rows.Add(dr1); 
    //} 
    dr1 = tasktable.NewRow(); 
    dr1[0] = "Sunit Shah"; 
    dr1[1] = System.DateTime.Now; 
    dr1[2] = "Test Note"; 
    dr1[2] = "Paul Pogba will have a medical at Manchester United on Monday after 
      Juventus granted permission for him to seal a potential world record 
      transfer.Juventus manager Massimo Allegri refused to be drawn further 
      on the transfer when he was asked about it in his post-match press 
      conference following a friendly against West Ham at the London Stadium, 
      but an official at the Italian club confirmed they had authorised the 
      midfielder to have United doctors assess him ahead of the move. 
      Allegri merely said: ‘I spoke about Pogba the day before, we have 
      just finished a match and I don’t know much more, we will see on Monday 
      if he is a still Juventus player.’"; 

    tasktable.Rows.Add(dr1); 
    DataTable fillNotesGrid = new DataTable(); 
    fillNotesGrid = (from row in tasktable.AsEnumerable() 
        orderby row.Field<DateTime>("Date") descending 
        select row).CopyToDataTable(); 
    dgvSIDetailsNotes.DataSource = fillNotesGrid;  
} 
+0

你能发布你用来显示网格的代码吗? – Interminable

+0

我刚刚添加了 – Hemil

+0

我会考虑你为什么会在gridview中显示上述文本。在自己的文本框中显示在网格之外可能会更好,当您选择不同的行时,该框会发生变化。 – Takarii

回答

0

据我所知,它看起来像你的问题不在于它截断数据,那就是它捕捉到细胞中,当您滚动,所以就在滚动时的每一个细胞的顶部的。如果它对于可用空间来说太大,则只能看到尽可能多的该行的顶部。基本上,它的行为就像Microsoft Excel。

我不认为有一种方法可以关闭标准DataGridView控件中的这种行为。但是,作为一种解决方法,您可以将DataGridView添加到System.Windows.Forms.Panel,并使Panel为您滚动!

简单地使DataGridViewPanel控制的孩子,和PanelAutoScroll属性trueDataGridViewAutoSize属性设置为true和应该做的伎俩。

编辑

我已经包括了一些源代码,试图把事情说清楚。

这是表格的源代码(其主体部分为FillGrid()),因为它与您问题中的源代码相同)。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True; //I'm assuming this is already set in your own source code as the cell is wrapping its text in your screenshot. 
     panel1.AutoScroll = true; 
     dataGridView1.AutoSize = true; 

     FillGrid(); 
    } 

    void FillGrid() 
    { 
     ... 
    } 
} 

这是控制如何在Form1.Designer.cs创建:

private System.Windows.Forms.DataGridView dataGridView1; 
private System.Windows.Forms.Panel panel1; 

这是在设计器所生成的InitializeComponent()方法。

private void InitializeComponent() 
    { 
     this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
     this.panel1 = new System.Windows.Forms.Panel(); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
     this.panel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // dataGridView1 
     // 
     this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
     this.dataGridView1.Location = new System.Drawing.Point(3, 3); 
     this.dataGridView1.Name = "dataGridView1"; 
     this.dataGridView1.Size = new System.Drawing.Size(145, 122); 
     this.dataGridView1.TabIndex = 0; 
     // 
     // panel1 
     // 
     this.panel1.Controls.Add(this.dataGridView1); 
     this.panel1.Location = new System.Drawing.Point(12, 12); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(260, 126); 
     this.panel1.TabIndex = 1; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 261); 
     this.Controls.Add(this.panel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
     this.panel1.ResumeLayout(false); 
     this.ResumeLayout(false); 

    } 

这应该是您在测试项目中复制此解决方案所需的一切。

如果您在主项目中遇到问题,那么您可能会遇到其他干扰的代码。

+0

我们已经尝试过了。但它不起作用。 我们添加了一个面板并将其AutoScroll属性保留为True 保留数据Gridview滚动条为无 并在运行时将Autosize保持为true。 还是一样的行为。不知道我在做什么错 – Hemil

+0

@Hemil我已经更新了答案,包括一些应该帮助你的源代码。如果您仍然遇到问题,您将不得不进一步提供有关您正在做什么,您正在处理的内容等的更多信息。 – Interminable