PDA

View Full Version : هایلایت کردن کلمه بعد از جستجو در datagridview



ya ali
سه شنبه 01 دی 1388, 16:49 عصر
سلام کسی میدونه چطور یک کلمه را در یک متن موجود در دیتاگریدویو پس از اینکه جستو کردم کلمه موردنظر را نیز هایلایت یا متمایز از بقیه کلمات کنم . ممنون

Soroush.Sarabi
سه شنبه 01 دی 1388, 17:45 عصر
دوست عزیز شما می توانید رنگ سلول مورد نظر رو تغییر بدهید.

ya ali
سه شنبه 01 دی 1388, 18:03 عصر
شرمنده برادر من یک متن بسیار زیادی در یک سلول دارم کهع می خواهم یک کلمه درآن رو مشخص کنم نه یک سلول رو لطفا کد مربوطه را بنویسید

ya ali
چهارشنبه 02 دی 1388, 07:32 صبح
سلام آقا یکی نیست که کدی مربوط به این رویداد رو در اختیار داشته باشه این کد مثل نحوه جستجوی در ورد می باشد ممنون یا علی

mohammad5593
چهارشنبه 22 آبان 1392, 15:57 عصر
سلام دوستان این تاپیک از سال 88 تا الان بدون پاسخ رها شده.
لطفا اگر کسی نحوه رنگ نمودن کلمه جستجو شده در گرید ویو یا همون هایلایت کردن را می دونه کمک کنه . نیاز دارم

بنده در یک تاپیک نحوه انجام این کار البته روی textbox را دیدم اما به کارم نیومد. لطفا کمک کنید

mohammadsamadi1377
چهارشنبه 22 آبان 1392, 17:21 عصر
یه پیشنهاد :
میتونی متن رو بریزی تو یه RichTextBox بعد از اون جا هایلایتش کنی . :لبخندساده:

Mahmoud.Afrad
چهارشنبه 22 آبان 1392, 18:18 عصر
من با یه جستجو اینو پیدا کردم:

string sw = null;
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (!(e.RowIndex >= 0 & e.ColumnIndex >= 0))
{
return;
}
e.Handled = true;
e.PaintBackground(e.CellBounds, true);

DataGridView dgv = sender as DataGridView;

if (!string.IsNullOrEmpty(sw))
{
string val = e.FormattedValue.ToString();

int sindx = val.ToLower().IndexOf(sw.ToLower());
if (sindx >= 0)
{
//the highlite rectangle
Rectangle hlRect = new Rectangle
{
Y = e.CellBounds.Y + 2,
Height = e.CellBounds.Height - 5
};

//find the size of the text before the search word
//and the size of the search word
string sBefore = val.Substring(0, sindx);
string sWord = val.Substring(sindx, sw.Length);
Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size);
Size s2 = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size);

//adjust the widths to make the highlite more accurate
if (s1.Width > 5)
{
hlRect.X = e.CellBounds.X + s1.Width - 5;
hlRect.Width = s2.Width - 6;
}
else
{
hlRect.X = e.CellBounds.X + 2;
hlRect.Width = s2.Width - 6;
}

//use darker highlight when the row is selected
SolidBrush hlBrush = default(SolidBrush);
hlBrush = ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.None) ? new SolidBrush(Color.DarkGoldenrod) : new SolidBrush(Color.LightGoldenrodYellow);

//paint the background behind the search word
e.Graphics.FillRectangle(hlBrush, hlRect);

hlBrush.Dispose();
}
}

//paint the content as usual
e.PaintContent(e.CellBounds);
}

کلمه مورد جستجو از طریق تکست باکس وارد میشه:

private void textBox1_TextChanged(object sender, EventArgs e)
{
sw = textBox1.Text;
dataGridView1.Invalidate();
}


البته کد بالا فقط یک کلمه در هر سلول رو رنگ میکنه، یعنی اگر دو کلمه در یک سلول باشند که باید رنگ بشن فقط اولی رنگ میشه که میتونی کدها رو تغییر بدی تا این مشکل هم رفع بشه.

mohammad5593
پنج شنبه 23 آبان 1392, 09:10 صبح
سلام چرا برای من کار نمیکنه میشه لطفا فایل نمونه را بگذارید

Mahmoud.Afrad
پنج شنبه 23 آبان 1392, 11:37 صبح
این هم کد اصلاح شده که تمام کلمات رو مشخص میکنه:

private string sw = null;

private void textBox1_TextChanged(object sender, EventArgs e)
{
sw = textBox1.Text;
dataGridView1.Invalidate();
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (!(e.RowIndex >= 0 & e.ColumnIndex >= 0))
{
return;
}
e.Handled = true;
e.PaintBackground(e.CellBounds, true);

if (!string.IsNullOrEmpty(sw))
{
string val = e.FormattedValue.ToString();
string tempVal = val.ToLower();
string tempSw = sw.ToLower();
List<int> indexes = new List<int>();
// find first word index
int sindx = tempVal.IndexOf(tempSw);
if (sindx >= 0)
{
indexes.Add(sindx);
}
// find another word indexes
while ((sindx = tempVal.IndexOf(tempSw, sindx + tempSw.Length)) >= 0)
{
indexes.Add(sindx);
}

if (indexes.Count > 0)
{
//the highlite rectangle
Rectangle hlRect = new Rectangle
{
Y = e.CellBounds.Y + 2,
Height = e.CellBounds.Height - 5
};

foreach (int indx in indexes)
{
//find the size of the text before the search word
//and the size of the search word
string sBefore = val.Substring(0, indx);
string sWord = val.Substring(indx, sw.Length);
Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size);
Size s2 = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size);

//adjust the widths to make the highlite more accurate
if (s1.Width > 5)
{
hlRect.X = e.CellBounds.X + s1.Width - 5;
hlRect.Width = s2.Width - 6;
}
else
{
hlRect.X = e.CellBounds.X + 2;
hlRect.Width = s2.Width - 6;
}

//use darker highlight when the row is selected
SolidBrush hlBrush = default(SolidBrush);
hlBrush = ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.None)
? new SolidBrush(Color.DarkGoldenrod)
: new SolidBrush(Color.LightGoldenrodYellow);

//paint the background behind the search word
e.Graphics.FillRectangle(hlBrush, hlRect);

hlBrush.Dispose();
}
}
}

//paint the content as usual
e.PaintContent(e.CellBounds);
}


نمونه پروژه رو هم مییتونید دانلود کنید

mohammad5593
پنج شنبه 23 آبان 1392, 15:32 عصر
چرا فقط یک حرف را انجام میده ولی حرف دوم به بعد ایراد میگریه
مثلا اگر در گریدویو بنویسیم علی و توی تکست باکس حرف "ع" مشکلی نداره اما "عل" را ایراد میگیره
112806

devotion
چهارشنبه 21 خرداد 1393, 16:48 عصر
این کدا برای دیتا گرید ویو چپ چینه، برای دیتا گرید ویو راست چین چه تغییری بدم؟
کلا جای کلمه رو اشتباه نشون میده.
لطفا راهنمایی کنید