2013-02-22 61 views
0

我有一個多行的DataGridView,你會如何找到字符串在Cell.Value字符串中的位置?如何找到一行在dataGridView單元格中的位置?

+0

閱讀http://stackoverflow.com/faq – nawfal 2013-02-22 14:24:05

+0

取決於您的列寬:http://msdn.microsoft.com/de-de/library/system.windows.forms.datagridviewcolumn.width.aspx – jAC 2013-02-22 14:24:30

回答

1
string testLength = "1"; 
    private void DataSheets_Load(object sender, EventArgs e) //DataGridView, 2 columns 
    { 
     clmData.DefaultCellStyle.WrapMode = DataGridViewTriState.True; 
     Data.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 
     Data.DefaultCellStyle.Font = new Font("Courier", 8); 
     RunWidth(); 
    } 

    void RunWidth() 
    { 
     int l1 = len(testLength); 
     int l2 = len(testLength + testLength); 
     int perChar = l2 - l1; 
     int pad = l1 - perChar; 
     int s7 = pad + (perChar * 75); 
     int s5 = pad + (perChar * 50); 
     Data.Columns[0].Width = s7; 
     Data.Columns[1].Width = s5; 
    } 
    public int len(string text) 
    { 
     Size te = TextRenderer.MeasureText(text, new Font("Courier", 8)); 
     return te.Width; 
    } 

我自己解決了這個問題,但如果有人找到更多的方法來找到類似的功能,請繼續發佈結果。

+1

YOu可以接受你自己的帖子 – Kinected 2013-02-24 02:18:34

0

只是因爲它有很多的好消息:
Here's a very well elaborated answer on differences between TextRenderer.MeasureText() and Graphics.MeasureString()

我有一個類似的問題;我想知道有多少行被包裹when drawing a DataGridViewRow by myself創建(代碼是C++/CLI):

void PaintRow (Object^ sender, DataGridViewRowPrePaintEventArgs^ e) 
{ 
... 
    int iCharacters = 0, iLines = 0; 
    SizeF oLayoutArea ((float)dgvRow->Cells[ixCell]->Size.Width, 0); 
    SizeF oTextSize = e->Graphics->MeasureString (sText, 
             dgvRow->Cells[ixCell]->InheritedStyle->Font, 
             oLayoutArea, 
             oStringFormat, 
             iCharacters, 
             iLines); 

關於你的問題:
我將採取相同的代碼,並找出有多少行是由完整的創建字符串,例如2行。
然後取一半字符串,並檢查你得到的行數,例如1行。
因爲之前的行數不多,我會再次讀取更多文本(總是差異的一半)並再次測試。 繼續下去,直到你把它分解成一個字符,並得到你的結果長度適合沒有包裝。

相關問題