2011-01-26 11 views

답변

1

그래서 당신은 GRIDVIEW있는 페이지를 가지고 :

<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="true"> 
</asp:GridView> 

<div> 
    This is where the count of rows of your GridView will be displayed: 
    <p> 
     <strong><asp:Label runat="server" ID="lCount" /></strong> 
    </p> 
</div> 

그리고, 코드 숨김에서 당신은 다음과 같이 채 웁니다 :

this.gv1.DataSource = FullName.GetDemoCollection(); //Just returns a List<string>; 
gv1.DataBind(); 

그리고 당신과 함께 무언가를 다른 클래스 GridViewRowCounter이 GridView, 예. 행을 계산 :

public partial class PassingControls : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //Bind the GridView 
     this.gv1.DataSource = FullName.GetDemoCollection();// 
     gv1.DataBind(); 

     //Pass Gridview reference to the GridVeiwRowCounter constructor. 
     GridViewRowCounter gvcounter = new GridViewRowCounter(this.gv1); 
     //Get the external class to return the rowcount from your GridView. 
     this.lCount.Text = gvcounter.GetRowCount().ToString(); 
    } 
} 

HTH :

그래서
public class GridViewRowCounter 
{ 
    private System.Web.UI.WebControls.GridView _gv; 

    public GridViewRowCounter(){} 

    public GridViewRowCounter(System.Web.UI.WebControls.GridView _GV){ 
     this._gv = _GV; 

    } 

    public int GetRowCount(){ 
     return _gv.Rows.Count; 
    } 

} 

, 당신은 같은 것을 할 수있는 Gridviewcounter 클래스로의 GridView를 전달합니다.

I 희망이 당신은

+0

저에게 대답하기 위해 시간을내어 주셔서 감사합니다. 그러나, 죄송합니다! u는 사실은 내가이 클래스 MyClass에 같은 클래스 뭔가 { 공공 MyClass에 (GRIDVIEW의 GV) { // 일부 코드 } } 위의 코드는 필요에 매개 변수로 GRIDVIEW을 전달하려는 내 질문에. :-)를 잘못 해석 수업에 참여하십시오. Gridview 나는 .aspx 페이지에서 전달하고 싶습니다. 일부 네임 스페이스 생성자에서 데이터 형식으로 gridview 얻을 수있는 클래스에 추가해야 할 것 같아요. 이 작업을 수행하는 방법? – Swathi

+0

정말 혼란 스럽네요. 위의'GridViewRowCounter' 클래스의 코드를 살펴보면 2 개의 생성자가 있습니다. 하나는 매개 변수를 사용하지 않는 기본값이고 다른 하나는 다음과 같습니다. 'public GridViewRowCounter (System .Web.UI.WebControls.GridView _GV) { this._gv = _GV; }' GridView를 매개 변수로 사용합니다. 이게 너가 의미 하는게 아니야? – 5arx

+0

나는 클래스에서 키워드 gridview를 가져올 수 없다는 것을 의미합니다. – Swathi

관련 문제