2011-01-10 8 views
20

मैं एक बहु थ्रेडेड एप्लिकेशन बना रहा हूं जिसमें प्रत्येक थ्रेड मेरे DataGridView में एक पंक्ति के रूप में दिखाई देगा। मैं प्रत्येक पंक्ति में ProgressBar चाहता हूं जो संबंधित थ्रेड प्रगति को इंगित करता है।टेक्स्ट और प्रोग्रेसबार के साथ डेटाग्रिड व्यू पॉप्युलेट करना

सवाल यह है, क्या यह संभव है? और यदि हां, तो कैसे?

+2

MSDN नमूना कोड: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/769ca9d6-1e9d-4d76-8c23-db535b2f19c2 मुझे उसके बाद मल्टीथ्रेडिंग में कोई समस्या नहीं दिखाई दे रही है। –

उत्तर

29

मैं कक्षा DataGridViewProgressColumn.cs जोड़ा (स्रोत: MSDN)

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
using System.ComponentModel; 
namespace Sample 
{ 
    public class DataGridViewProgressColumn : DataGridViewImageColumn 
    { 
     public DataGridViewProgressColumn() 
     { 
      CellTemplate = new DataGridViewProgressCell(); 
     } 
    } 
} 
namespace Sample 
{ 
    class DataGridViewProgressCell : DataGridViewImageCell 
    { 
     // Used to make custom cell consistent with a DataGridViewImageCell 
     static Image emptyImage; 
     static DataGridViewProgressCell() 
     { 
      emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
     } 
     public DataGridViewProgressCell() 
     { 
      this.ValueType = typeof(int); 
     } 
     // Method required to make the Progress Cell consistent with the default Image Cell. 
     // The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int. 
     protected override object GetFormattedValue(object value, 
          int rowIndex, ref DataGridViewCellStyle cellStyle, 
          TypeConverter valueTypeConverter, 
          TypeConverter formattedValueTypeConverter, 
          DataGridViewDataErrorContexts context) 
     { 
      return emptyImage; 
     } 
    protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) 
    { 
     try 
     { 
      int progressVal = (int)value; 
      float percentage = ((float)progressVal/100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%. 
      Brush backColorBrush = new SolidBrush(cellStyle.BackColor); 
      Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor); 
      // Draws the cell grid 
      base.Paint(g, clipBounds, cellBounds, 
      rowIndex, cellState, value, formattedValue, errorText, 
      cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground)); 
      if (percentage > 0.0) 
      { 
       // Draw the progress bar and the text 
       g.FillRectangle(new SolidBrush(Color.FromArgb(203, 235, 108)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4); 
       g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X+(cellBounds.Width/2)-5, cellBounds.Y + 2); 

      } 
      else 
      { 
       // draw the text 
       if (this.DataGridView.CurrentRow.Index == rowIndex) 
        g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2); 
       else 
        g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2); 
      } 
     } 
     catch (Exception e) { } 

    } 
} 
} 

तब मैं GridView

DataGridViewProgressColumn column = new DataGridViewProgressColumn(); 

kryptonDataGridView1.ColumnCount = 2; 
kryptonDataGridView1.Columns[0].Name = "TESTHeader1"; 
kryptonDataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
kryptonDataGridView1.Columns[1].Name = "TESTHeader22"; 
kryptonDataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
kryptonDataGridView1.Columns.Add(column); 
kryptonDataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
column.HeaderText = "Progress"; 


object[] row1 = new object[] { "test1", "test2", 50 }; 
object[] row2 = new object[] { "test1", "test2", 55 }; 
object[] row3 = new object[] { "test1", "test2", 22 }; 
object[] rows = new object[] { row1, row2, row3 }; 

foreach (object[] row in rows) 
{ 
    kryptonDataGridView1.Rows.Add(row); 
} 

और वह पॉप्युलेट करने के लिए कि कोड का इस्तेमाल किया परिणाम

alt text

+7

उत्तर बहुत उपयोगी है, लेकिन पहले भाग का स्रोत कोड एमएसडीएन को जिम्मेदार ठहराया जाना चाहिए: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/769ca9d6-1e9d-4d76-8c23 -db535b2f19c2 –

-2
<DataGridTemplateColumn Header="Progress" Width="*" Visibility="Visible"> 
    <DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
    <StackPanel> 
     <TextBlock Text="{Binding Progress}" HorizontalAlignment="Center" /> 
     <ProgressBar Value="{Binding Path=Progress, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Minimum="0" Maximum="100" Height="25" /> 
    </StackPanel> 
</DataTemplate> 

यह आपकी मदद करता है फिर से

DataGridView में प्रगति बार सेल के लिए
+0

@frzsombor हाँ, उपयोगी उत्तर नहीं। सवाल टैग किया गया है विंडोज़ फॉर्म WPF नहीं। आईएमएचओ, सवाल को बेहतर ढंग से हटा दें। – Kiquenet

संबंधित मुद्दे