C#.net怎样判断gridview中某一列的是否为空

2024-10-30 22:22:33
推荐回答(3个)
回答1:

我做了一个例子,测试通过了,绝对没有问题:
前台:






'>






后台:
private void Page_Load(object sender, EventArgs e)
{
//TextBox2.Text = "aaaaaaaa\r\nbbbbb";

if (!Page.IsPostBack)
{
//DateTime dt = DateTime.Now.ToString();

//GridView2.DataSource = ds;
//GridView2.DataBind();
DataSet ds = getSys();
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
Label lb1;
LinkButton lbl1;
for (int i = 0; i < GridView2.Rows.Count; i++)
{
lb1 = (Label)GridView2.Rows[i].FindControl("Label1");
lbl1 = (LinkButton)GridView2.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lbl1.Visible = false;
}
else //if (lb1.Text.Trim() == "True")
{
lbl1.Visible = true;
}
}
}

//int[] num = new int[5];
//string aa = "";
//for (i = 0; i < 5; i++)
//{
// num[i] = randoms();
// aa = aa + num[i].tostring();
// message.show(aa);
//}

}

public DataSet getSys()
{
try
{
//string FoxconnCAUserDBConnString = System.Configuration.ConfigurationManager.AppSettings["FCARegConnString"].ToString();
string DBConnString = "Data Source=10.153.26.252;Initial Catalog=FCAReg;Persist Security Info=True;User ID=causer;Password=foxconnca";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(DBConnString);
string pSQL = "select * from ref_usersreg_sys";
SqlCommand cmd = new SqlCommand(pSQL, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
return ds;
}
catch
{
return null;
}
}

回答2:

protected void GridView1_OnDataBound(object sender, GridViewRowEventArgs e)
{
if(String.IsNullOrEmpty(((Label)e.Row.FindControl("Label1")).Text))
e.Row.FindControl("Label1").Visible = false;
else
e.Row.FindControl("LinkButton").Visible = false;
}

回答3:

for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lb1 = (Label)GridView1.Rows[i].FindControl("Label1");
LinkButton lk1 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lk1.Visible =false;
}
else
{
lb1.Visible = false;
}
}