2013-02-22 59 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行。
因为之前的行数不多,我会再次读取更多文本(总是差异的一半)并再次测试。 继续下去,直到你把它分解成一个字符,并得到你的结果长度适合没有包装。

相关问题