2012-09-03 4 views
0

각 행에 GridView 및 편집 링크가 있습니다. 편집을 클릭하여 GridView 셀의 데이터를 채우고 GridView 행을 업데이트 할 수 있으며 데이터베이스의 해당 테이블도 업데이트됩니다.GridView에서 빈 셀을 무시하는 방법

나는 on_click이 각 행을 열 단위로 읽고 몇 가지 작업을 수행하는 저장 버튼이 있습니다.

GridView의 모든 셀에 일부 데이터가 채워져 있으면 함수가 제대로 작동합니다. GridView의 셀이 비어 있으면 오류가 발생합니다. 입력 문자열의 형식이 올바르지 않습니다.

내가 시도하는 코드는 다음과 같습니다

protected void SAVE_GRID_Click(object sender, EventArgs e) 
    { 
     foreach (GridViewRow row in GridView2.Rows) 
     { 
      string Loc = row.Cells[1].Text; 

      string strg = "SELECT Location_Type FROM Quantity WHERE Locations='" + Loc + "'"; 
      SqlCommand com = new SqlCommand(strg, con); 
      con.Open(); 
      SqlDataReader sdr = com.ExecuteReader(); 
      while (sdr.Read()) 
      { 
       Loctype.Text = sdr[0].ToString().Trim(); 
      } 
      con.Close(); 

      for (int i = 1; i < GridView2.Columns.Count; i++) 
      { 
       String header = GridView2.Columns[i].HeaderText;     

       string str = "SELECT Profile_Type FROM Profile_Tooltip WHERE Profile_Name='"+header+"'"; 
       SqlCommand cmd = new SqlCommand(str,con); 
       con.Open(); 
       SqlDataReader dr = cmd.ExecuteReader(); 
       while (dr.Read()) 
       { 
        profiletype.Text = dr[0].ToString().Trim(); 
       } 
       con.Close(); 

       if (!string.IsNullOrEmpty(row.Cells[i + 1].Text.Trim())) 
       { 
        int n = Convert.ToInt16(row.Cells[i + 1].Text); 
        //int n = int.Parse(row.Cells[i].Text); 

        for (int m = Asset_List.Items.Count - 1; m >= 0; m--) 
        { 
         Asset_List.Items.Remove(Asset_List.Items[m]); 
        } 

        for (int j = 1; j <= n; j++) 
        { 
         Asset_List.Items.Add(string.Concat(profiletype.Text, j)); 
        } 

        for (int k = 0; k <= Asset_List.Items.Count - 1; k++) 
        { 
         com = new SqlCommand("INSERT INTO " + Label3.Text + " VALUES ('" + Loctype.Text + "','" + Loc + "','" + header + "','" + Asset_List.Items[k] + "')", con); 
         con.Open(); 
         com.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 
      } 
     } 
     SqlCommand comd = new SqlCommand("SELECT * FROM " + Label3.Text + "", con); 
     SqlDataAdapter da = new SqlDataAdapter(comd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

나는 셀이 비어있는 경우 내가 빈 셀에 대한 조치를 수행하지 않고 다음 셀로 증가 할 비어 있는지 확인하고 싶습니다.

친절하게이 문제를 해결하는 데 도움이됩니다. 고맙습니다.

+0

* 휴식 *을 제거하십시오. – adatapost

+0

여전히 동일한 오류 –

+0

내 질문에 오류가 언급 된 –

답변

3

그냥 당신이 문자열을 정수로 변환하거나 해석 또는 프레임 워크의 tryparse 방법을 사용하지 여기

int n = Convert.ToInt16(row.Cells[i + 1].Text); 

검사를 확인해야 트림과 고통 기능을

if (!string.IsNullOrEmpty(row.Cells[i].Text.Trim())) 

를 사용

short n = 0; 
if(Int16.TryParse(row.Cells[i + 1].Text,out n);) 
{ 
    //perform function and user n here now 
} 
+0

여전히 동일한 오류 –

+0

내 전체 코드는 내 질문에 업데이트되었습니다. 정확히 내가하고있는 와트를 보여줍니다. –

+0

그래서 int n = Convert.ToInt16 (row.Cells [i + 1] .Text);을 바꿔야합니다. ~ int n = 0; if (TryParse.int (row.Cells [i + 1] .Text, out n);) { // 여기서 함수와 사용자 n을 수행하십시오. } ?????? –

0

나에게 가장 잘 어울리는 라인은 다음과 같습니다.

int n = Convert.ToInt16(row.Cells[i + 1].Text); 

나는 보통 예외를 방지하기 위해이 같은 것을 사용

int n = 0; 
bool didParse = Int16.TryParse(row.Cells[i + 1].Text, out n); 
if (didParse) 
{ 
    //add code here 
} 

귀하의 오류는 예외가 휴대 텍스트가 비어 있거나 심지어는 null이기 때문에 던져지는 수 있습니다.

+0

이렇게 나에게 오류가 발생합니다 : 오류 1 : 'short.TryParse (string, out short)'에 가장 적합한 오버로드 된 메서드는 잘못된 인수가 있습니다. 오류 \t 2 : 인수 2 : 'out int'에서 'short short'로 변환 할 수 없습니다. –

+0

Int16 n = 0인데 죄송합니다. –

관련 문제