2012-03-15 13 views
35

में ग्रिड व्यू में इंडेक्स द्वारा कॉलम नाम से सेल वैल्यू कैसे प्राप्त करें Ip.net में gridview है और अब मैं सेल नाम कॉलम नाम द्वारा सेल इंडेक्स द्वारा नहीं चाहता हूं।एएसपीनेट

कैसे सेल स्तंभ नाम

+0

जब आप उस मूल्य को चाहते हैं? पृष्ठ में या जमा करने के बाद ग्रिड प्रस्तुत किया जाता है? – balexandre

+0

सबमिट करने के बाद केवल –

उत्तर

57

GridView के रूप में स्तंभ नाम कार्य नहीं करता, के रूप में बस इतना ही द्वारा सेल मूल्य पुन: प्राप्त करने से यह संभव हो जाएगा datasource संपत्ति उन चीजों को पता करने के लिए।

यदि आपको अभी भी एक कॉलम नाम दिए गए इंडेक्स को जानने की आवश्यकता है, तो आप इसे gridview के रूप में करने के लिए एक सहायक विधि बना सकते हैं शीर्षलेख में आमतौर पर यह जानकारी होती है।

int GetColumnIndexByName(GridViewRow row, string columnName) 
{ 
    int columnIndex = 0; 
    foreach (DataControlFieldCell cell in row.Cells) 
    { 
     if (cell.ContainingField is BoundField) 
      if (((BoundField)cell.ContainingField).DataField.Equals(columnName)) 
       break; 
     columnIndex++; // keep adding 1 while we don't have the correct name 
    } 
    return columnIndex; 
} 

याद रखें कि कोड के ऊपर एक BoundField का उपयोग करेगा ... तो की तरह उपयोग:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int index = GetColumnIndexByName(e.Row, "myDataField"); 
     string columnValue = e.Row.Cells[index].Text; 
    } 
} 

मैं दृढ़ता से सुझाव है कि आप TemplateField उपयोग करते समय स्वयं नियंत्रित किए जाने की, तो यह आसान है जैसे उन नियंत्रणों हड़पने:

<asp:GridView ID="gv" runat="server"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

और उसके बाद का उपयोग

string columnValue = ((Label)e.Row.FindControl("lblName")).Text; 
+0

यह एक जीवन बचाता है! धन्यवाद – Artin

+0

सहमत हुए। जीवन सेवर। धन्यवाद! यह मेरे "उपयोगिता सामान के छोटे बैग" में जा रहा है – pStan

+0

@balexandre क्या यह एक टेम्पलेटफिल्ड है? मेरे पास एक पंक्ति में बाउंडफिल्ड और टेम्पलेटफिल्ड दोनों हैं। –

2

हालांकि इसकी एक लंबे समय लेकिन कोड के इस अपेक्षाकृत छोटा सा टुकड़ा पढ़ सकते हैं और प्राप्त करना आसान लगता है:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int index; 
    string cellContent; 

    foreach (TableCell tc in ((GridView)sender).HeaderRow.Cells) 
    { 
     if(tc.Text.Equals("yourColumnName")) 
     { 
     index = ((GridView)sender).HeaderRow.Cells.GetCellIndex(tc); 
     cellContent = ((GridView)sender).SelectedRow.Cells[index].Text; 
     break; 
     } 
    } 
} 
2

आप DataRowView उपयोग कर सकते हैं स्तंभ अनुक्रमणिका मिलता है।

void OnRequestsGridRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      var data = e.Row.DataItem as DataRowView; 

      // replace request name with a link 
      if (data.DataView.Table.Columns["Request Name"] != null) 
      { 
       // get the request name 
       string title = data["Request Name"].ToString(); 
       // get the column index 
       int idx = data.Row.Table.Columns["Request Name"].Ordinal; 

       // ... 

       e.Row.Cells[idx].Controls.Clear(); 
       e.Row.Cells[idx].Controls.Add(link); 
      } 
     } 
    } 
2

लैम्ब्डा प्रेमियों के लिए

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var boundFields = e.Row.Cells.Cast<DataControlFieldCell>() 
      .Select(cell => cell.ContainingField).Cast<BoundField>().ToList(); 

     int idx = boundFields.IndexOf(
      boundFields.FirstOrDefault(f => f.DataField == "ColName")); 

     e.Row.Cells[idx].Text = modification;   
    } 
} 
0

सिकंदर के जवाब में indexcolumn साथ एक छोटी सी बग: हम "नहीं मिला" कॉलम की देखभाल की जरूरत है:

int GetColumnIndexByName(GridViewRow row, string columnName) 
{ 
    int columnIndex = 0; 
    int foundIndex=-1; 
    foreach (DataControlFieldCell cell in row.Cells) 
    { 
     if (cell.ContainingField is BoundField) 
     { 
      if (((BoundField)cell.ContainingField).DataField.Equals(columnName)) 
      { 
       foundIndex=columnIndex; 
       break; 
      } 
     } 
     columnIndex++; // keep adding 1 while we don't have the correct name 
    } 
    return foundIndex; 
} 

और

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int index = GetColumnIndexByName(e.Row, "myDataField"); 
     if(index>0) 
     { 
      string columnValue = e.Row.Cells[index].Text; 
     } 
    } 
} 
0

Code Project

पर मिलने वाली किसी चीज़ के आधार पर ग्रिड के डेटा स्रोत के आधार पर डेटा तालिका घोषित हो जाने के बाद, कॉलम संग्रह से कॉलम नाम से कॉलम अनुक्रमणिका देखें। इस बिंदु पर, सेल को प्रारूपित करने या प्रारूपित करने के लिए आवश्यक इंडेक्स का उपयोग करें।

protected void gridMyGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DataTable dt = (DataTable)((GridView)sender).DataSource; 
     int colIndex = dt.Columns["MyColumnName"].Ordinal; 

     e.Row.Cells[colIndex].BackColor = Color.FromName("#ffeb9c"); 
    } 
} 
+0

कृपया सिर्फ सादा कोड पोस्ट न करें। आगे बढ़ें और अपने समाधान की व्याख्या करें। –