2012-04-16 2 views
0

여러 페이지에서 GridView의 열 이름의 이름을 바꾸고 싶습니다. 지금까지 한 번에 하나씩 변경할 수 있습니다. 내 코드는 다음과 같습니다.여러 페이지에서 GridView의 HeaderText를 변경하는 방법은 무엇입니까?

protected void Button1_Click(object sender, EventArgs e) 
{ 

    for (int i = 0; i < GridView1.Columns.Count; i++) 
    { 
     if (GridView1.Columns[i].HeaderText == TextBox1.Text) 
     { 
      GridView1.Columns[i].HeaderText = TextBox2.Text; 
     } 

    } 


} 

두 개의 텍스트 상자와 버튼이 있습니다. 단추를 클릭하면 textbox1이 표시되고 textbox2에 입력 한 열의 이름이 바뀝니다.

내가하고자하는 것 : 여러 페이지가 있고 그 열 머리글을 바꾸고 싶습니다. 이 버튼과 텍스트 상자를 각 페이지에 추가하고 싶지 않습니다. 이 함수를 한 번만 호출하고 모든 페이지의 HeaderText를 변경하는 방법이 있습니까?

미리 감사드립니다.

답변

0

데이터베이스에 열 이름을 보내서 해결했으며 Page_Load에서 데이터베이스의 새로운 헤더를 가져옵니다. 이것이 최선의 해결책이 아니라는 것을 알고 있습니다. 매번 페이지가로드 될 때마다 데이터베이스에 연결되기 때문에 속도가 느립니다. 아무것도없는 것보다는 낫다. :)

데이터베이스 부분 :

try 
    { 
     con.Open(); 
     string[] _col = { }; // Enter here the column names 
     for (int i = 0; i < _col.Length; i++) 
     { 
      if (_col[i] == TextBox1.Text) 
      { 
       SqlCommand ins = new SqlCommand("insert into Kolon_Ismi_Degistirme (ind, newHeaderName) values (@ind, @newHeaderName)", con); 
       ins.Parameters.AddWithValue("@ind", i); 
       ins.Parameters.AddWithValue("@newHeaderName", TextBox2.Text); 
       ins.ExecuteNonQuery(); 
      } 
     } 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
    finally 
    { 
     con.Close(); 
    } 

를 Page_Load 부분

try 
    { 
     //Get new column name using database       
     con.Open(); 
     SqlCommand sel = new SqlCommand("select ind, newHeaderName from Kolon_Ismi_Degistirme", con); 
     reader = sel.ExecuteReader(); 
     while (reader.Read()) 
     { 
      GridView1.Columns[(int)reader["ind"] + 2].HeaderText = (string)reader["newHeaderName"]; 
     }     
    } 
    catch (global::System.Exception) 
    { 
     throw; 
    } 
    finally 
    { 
     reader.Close(); 
     con.Close(); 
    } 
관련 문제