14 Mayıs 2013 Salı

Gridview kolon adı bilgisine ulaşmak

Gridview kolon adlarına ulaşmak için kod ;

Hardcoded approach:
string searchValue = "SpecifiedName";
// where 1 is the hardcoded cell index
var query = from GridViewRow row in GridView1.Rows
            where row.Cells[1].Text == searchValue
            select row;
GridViewRow result = query.FirstOrDefault();
Dynamic Approach (Column Index Lookup):
string colName = "name";
int index = (from DataControlField col in GridView1.Columns
            where col.HeaderText == colName
            select GridView1.Columns.IndexOf(col)).FirstOrDefault();

// index used
var query = from GridViewRow row in GridView1.Rows
        where row.Cells[index].Text == searchValue
        select row;
GridViewRow result = query.FirstOrDefault();
Alternate index lookup: instead of using HeaderText you can use BoundField.
int index = (from DataControlField col in GridView1.Columns
            where ((BoundField)col).DataField == colName
            select GridView1.Columns.IndexOf(col)).FirstOrDefault();

WinForm DataGridView

Kept this here just in case.
string name = "SpecifiedName";
var query = from DataGridViewRow row in dataGridView1.Rows
            where row.Cells["name"].Value.ToString() == name
            select row;
// the row will be returned by this or contain a default value if not found
DataGridViewRow result = query.FirstOrDefault();
Share:

0 yorum:

Yorum Gönder

Blog Arşivi