2012-06-11 2 views
1

다음은 GridView 컨트롤을 채우는 방법입니다. 나는 .aspx 프론트 엔드가 아닌 코드 숨김에서 이것을 수행하고있다. 다음은 내가 무슨의 매우 축약 버전 :RowDataBound 이벤트의 한 줄이 Gridview의 호출 메커니즘과 충돌합니다

private void UpdateGridView() 
{ 
    DataTable temptable = new DataTable(); 
    DataColumn idcol = new DataColumn(); 
    DataColumn titlecol = new DataColumn(); 
    idcol.ColumnName = "ID"; 
    titlecol.ColumnName = "Title"; 
    temptable.Columns.Add(idcol); 
    temptable.Columns.Add(titlecol); 

...(get data from the database, store it as variable "x")... 

    DataRow tempdr; 
    tempdr[idcol] = x.ID; 
    tempdr[titlecol] = x.Title; 

    temptable.Rows.Add(tempdr); 

    GridView1.DataSource = temptable; 
    GridView1.DataBind(); 
} 

의 GridView의 "AllowPaging이"true로 설정하고, 나는 다음과 같은 이벤트 핸들러가 페이징 처리하려면 :

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    UpdateGridView(); 
} 

을 그리고이 위대한 작품을 !

그러나 나는 또한 RowDataBound 이벤트 핸들러를 가지고 : 여기

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[0].Visible = false; //hide the ID 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
     e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
     e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
    } 
} 

내 목표는 자신이 클릭 할 행을 가지고 있고, 그 행의 ID와 동일한 쿼리 문자열을 다른 페이지로 연결하는 것입니다. ID 열에 값을 입력해야 행을 만들 때 액세스 할 수 있으므로 링크의 QueryString에 ID를 추가 할 수 있습니다. 하지만 ID 열이 보이기를 원하지 않기 때문에 다음 줄을 추가했습니다. e.Row.Cells[0].Visible = false; 이렇게하면 페이징 기능이 중단됩니다. 페이지 번호가 더 이상 표시되지 않습니다. 이 한 줄을 주석으로 처리하면 모든 것이 작동하지만 ID가 GridView에 표시됩니다.

1) 이유는 무엇입니까? 2) 동일한 기능을 사용하기 위해 할 수있는 일은 무엇입니까?

답변

0

많은 시행 착오 끝에 그것을 알아 냈습니다. 이것은 극히 드문 문제로 보입니다. 조건이 매우 구체적이기 때문에 인터넷에 관한 정보는별로 없습니다. 내가 읽은 대부분의 것은 DataViewNames를 대신 사용하여 GridView에 ID를 일반 열로 넣는 대신에 이야기했습니다.

나는 효과가있을 것이라고 확신하지만 이미 많은 시간을 낭비 했으므로 최대한 빨리 솔루션을 필요로했습니다. 당신은 내가 내 원래의 질문에 있었다으로 어떤 조건 e.Row.Cells[0].Visible = false;를 사용하는 경우, 어떻게 든 표시로와 호출기 보이지 않게하는 것으로 해석

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 
    { 
     e.Row.Cells[0].Visible = false; //hide the ID 
     e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
     e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
     e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
    } 
} 

분명히 :

해결책은있는 GridViews RowDataBound 이벤트에있다 잘. 해결책은 if 블록에 e.Row.Cells[0].Visible = false; 행을 포함시키는 것이 었습니다.이 행에는 DataRow의 첫 번째 셀과 헤더 만 표시되며 호출기와 같은 다른 요소는 보이지 않습니다.

편집 : 방금 GridView 정렬을 사용하는 경우 위의 내용이 정렬에 영향을 미친다는 사실을 깨달았습니다. 정렬을 계속 사용하고 호출기를 표시하고 첫 번째 열을 숨기려면 다음과 같이하십시오.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 
    { 
     e.Row.Cells[0].Visible = false; //hide the ID 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
      e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'"; 
     } 
    } 
} 
관련 문제