2009-09-15 13 views
8

में स्वत: जेनरेटेड कॉलम छुपाएं मेरे पास एक ग्रिडव्यू है जो स्वत: जेनरेटेड कॉलम का उपयोग करता है, क्योंकि उपयोगकर्ता क्वेरी में वापस आने के लिए कॉलम का चयन कर सकता है। मैं पहचान के साथ कॉलम छिपाना चाहता हूँ। मैं स्वत: जनरेटेड कॉलम कैसे छिपा सकता हूं? यहां तक ​​कि डाटाबेस घटना में कॉलम गिनती शून्य है।ग्रिडव्यू

उत्तर

14

मैं ऐसा करने के तरीके की खोज की। आपको rowdatabound घटना का उपयोग करने और पंक्ति को बाध्य होने पर सेल को छिपाने की आवश्यकता है।

Protected Sub ResultGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ResultGrid.RowDataBound 
     e.Row.Cells(1).Visible = False 
End Sub 
+4

+1, केवल देखने के लिए चीज यह है कि आप इसे उचित रोटीप्स के लिए बदलते हैं। उदाहरण के लिए यदि (e.Row.RowType! = DataControlRowType.Pager) {e.Row.Cells [1]। दृश्यमान = झूठा; } –

0

मैं जांचता हूं कि कॉलम शून्य से अधिक था, यदि ऐसा है तो मैं इस तथ्य का उपयोग करूंगा कि कॉलम संग्रह को कॉलम नाम के साथ-साथ पहचान कॉलम को छिपाने के लिए पूर्णांक के संदर्भ में भी संदर्भित किया जा सकता है।

+2

स्वतः बनाए गए स्तंभ संग्रह में शामिल नहीं हैं लिया जाता है। – SchwartzE

+0

इसे डेटाबाउंड ईवेंट में करें, जिस बिंदु पर मुझे यकीन है कि स्वत: जेनरेट किए गए कॉलम कॉलम संग्रह में होंगे। – Lazarus

0

क्या आपको इसकी आवश्यकता है? सबसे सरल बात यह है कि इसे चुनिंदा क्वेरी में शामिल न करें।

आपको इसकी आवश्यकता और स्तंभ स्थिति जानते हैं:

gridView.Columns[KnownColumnIndex].Visible = false; 
+0

मुझे पंक्ति का चयन करने के लिए डेटाके के रूप में शामिल करने के लिए इंडेक्स की आवश्यकता है। – SchwartzE

+0

यह मेरे लिए काम नहीं करता है, gridView.Columns.Count Autogenerated कॉलम के लिए शून्य है। – Somebody

1
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[1].Visible = false; 
} 
1

मैं निम्नलिखित के साथ समस्या के हैक कर लिया है। मैंने सही स्तंभ सूचकांक देने के लिए सहायक कार्यों को लिखा है और फिर वांछित कॉलम को छुपाने के लिए लिखा है। एक बार सहायक कार्य करने के बाद, आप बस एक लाइनर को gridview_databound फ़ंक्शन से कॉल करते हैं।

protected void grd_DataBound(object sender, EventArgs e) 
{ 
    try 
    { 
     HideAutoGeneratedGridViewColumn(grd, "nContractID");   
    } 
    catch (Exception ex) 
    { 

    } 
} 

    public int getColumnIndex(GridView grd, string sColumnName) 
{ 
    return getColumnIndex(grd, sColumnName, false); 
} 
/// <summary> 
/// Returns the columns index of the specified column based on the header text. 
/// </summary> 
/// <param name="grd"></param> 
/// <param name="sColumnName"></param> 
/// <returns></returns> 
public int getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn) 
{ 
    int ReturnVal = -1; 
    try 
    { 
     if (grd != null) 
     { 
      if (!bAutoGeneratedColumn) 
      { 
       #region Static Columns 
       if (grd.Columns.Count > 0) 
       { 
        for (int x = 0; x < grd.Columns.Count; x++) 
        { 
         if (grd.Columns[x] != null) 
         { 
          if (grd.Columns[x].HeaderText.ToLower() == sColumnName.ToLower()) 
          { 
           ReturnVal = x; 
           break; 
          } 
         } 
        } 
       } 
       #endregion 
      } 
      else 
      { 
       #region AutoGenerated Columns 
       if (grd.HeaderRow != null) 
       { 
        for (int x = 0; x < grd.HeaderRow.Cells.Count; x++) 
        { 
         if (grd.HeaderRow.Cells[x] != null) 
         { 
          if (grd.HeaderRow.Cells[x].Text.ToLower() == sColumnName.ToLower()) 
          { 
           ReturnVal = x; 
           break; 
          } 
         } 
        } 
       } 
       #endregion 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     ReturnVal = - 1; 
     LogMessage("getColumnIndex(GridView grd, string sColumnName, bool bAutoGeneratedColumn) Error", ex.Message); 
    } 
    return ReturnVal; 
} 
/// <summary> 
/// Returns the columns index of the specified column based on the header text. 
/// </summary> 
/// <param name="sColumnName"></param> 
/// <param name="r"></param> 
/// <returns></returns> 
public int getColumnIndex(string sColumnName, GridViewRow r) 
{ 
    int ReturnVal = -1; 
    try 
    { 
     if (r != null) 
     { 
      if (r.Cells.Count > 0) 
      { 
       for (int x = 0; x < r.Cells.Count; x++) 
       { 
        if (r.Cells[x] != null) 
        { 
         if (((System.Web.UI.WebControls.DataControlFieldCell)(r.Cells[x])).ContainingField.HeaderText == sColumnName) 
         { 
          ReturnVal = x; 
          break; 
         } 
        } 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     ReturnVal = -1; 
    } 
    return ReturnVal; 
} 
public void HideAutoGeneratedGridViewColumn(GridView grd, string sColumnName) 
{ 
    HideAutoGeneratedGridViewColumn(grd, getColumnIndex(grd, sColumnName, true)); 
} 
public void HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex) 
{ 
    try 
    { 
     grd.HeaderRow.Cells[nColumnIndex].Visible = false; 
     for (int x = 0; x < grd.Rows.Count; x++) 
     { 
      grd.Rows[x].Cells[nColumnIndex].Visible = false; 
     } 
    } 
    catch (Exception ex) 
    { 
     LogMessage("HideAutoGeneratedGridViewColumn(GridView grd, int nColumnIndex) Error", ex.Message); 
    } 
} 
0

यह बिना यह डेटाबाउंड करना होगा की तरह में गड़बड़ दिखने ऑटोजनरेटेड स्तंभ शीर्ष लेख और सेल छुपा देगा। यह सही जवाब से here

Protected Sub Gdvisitor_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gdvisitor.RowCreated 
 
    If (e.Row.Cells.Count > 1) Then 
 
     e.Row.Cells(1).Visible = False 
 
    End If 
 
End Sub

संबंधित मुद्दे