การทำ highlight กับข้อความใน Gridview

เริ่มจาก add css ก่อนเลยครับ
<style type="text/css">.highlight {text-decoration:none; font-weight:bold;color:black; background:yellow;}style>
หลังจากนั้น ก็เข้าไปดูในส่วนของ Code behide
ส่วนของ gridview_RowDataBound
ให้ add โค้ดนี้ครับ

if (e.Row.RowType == DataControlRowType.DataRow)

{

TableCell cell = e.Row.Cells[1]; //เลือก Column ที่ ต้องการ

string cellText = cell.Text;

int leftIndex = cellText.IndexOf(Textbox1.Text, StringComparison.OrdinalIgnoreCase); //เปรียบเทียบคำระหว่างTextbox1.Text กับcellText

if (leftIndex >= 0)

{

int rightIndex = leftIndex + txtEmpName.Text.Length;

StringBuilder builder = new StringBuilder();

builder.Append(cellText, 0, leftIndex);

builder.Append("");

builder.Append(cellText, leftIndex, rightIndex - leftIndex);

builder.Append(""); builder.Append(cellText, rightIndex, cellText.Length - rightIndex);

cell.Text = builder.ToString();

}

ปล.เราสามารถเพิ่ม Column อื่นได้เลยนะครับ
}

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้