1

ツールチップをGridView割り当てるための DataBound 関数を記述した があります。しかし、割り当てられていません。私が書いた関数は次のとおりです。

SqlCommand comd = new SqlCommand("SELECT Location_Profile_Name, " + Label10.Text + " as Home_Profile FROM Home_Profile_Master", con);
        SqlDataAdapter da = new SqlDataAdapter(comd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView3.DataSource = dt;
        GridView3.DataBind();

protected void GridView3_DataBound(object sender, EventArgs e)
    {
        var gv = (GridView)sender;

        foreach (GridViewRow row in GridView3.Rows)
        {
            string label2 = row.Cells[2].Text.Trim();

            if (label2.Length != 0)
            {
                con.Open();
                string str = "SELECT Location_Profile_Tool_Tip FROM Location_Profile_List_ToolTip WHERE Location_Profile_Name='" + label2 + "'";
                SqlCommand cmd = new SqlCommand(str, con);

                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    row.Cells[2].ToolTip = dr[0].ToString().Trim();
                }
                con.Close();
            }
        }
    }

デバッグすると、label2は null です。同じコードが別のグリッドに対して実行されています。なにが問題ですか...!!助けてください..!

4

2 に答える 2

2

うーん...おそらくこれが問題ですか?

//                          **************
foreach (GridViewRow row in GridView3.Rows)

すべきですか?

//                          **
foreach (GridViewRow row in gv.Rows)

編集

ああ!Cells はゼロから始まる配列です。2 番目のセルが必要な場合は、配列インデックス 1 を使用する必要があります。

これ:

//                        *
string label2 = row.Cells[2].Text.Trim();

次のようにする必要があります。

//                        *
string label2 = row.Cells[1].Text.Trim();

編集

数値のセル インデックスを使用すると、読み取りが非常に難しく、非常に壊れやすくなります。列を追加したり、列を削除したりすると、すべてのコードが壊れます。次のように、セル名を使用することを強くお勧めします。

//                  ************
string label2 = row[Label10.Text].Text.Trim();

編集

多分これはあなたにとってよりうまくいくでしょうか?

string label2 = ( (DataRow) row.DataItem )[Label10.Text].ToString().Trim();
于 2012-09-28T13:58:55.687 に答える
0

TemplateField があり、ItemTemplate の ID を使用すると、問題が解決します。

protected void GridView3_DataBound(object sender, EventArgs e)
    {           
        foreach (GridViewRow row in GridView3.Rows)
        {
            Label label1 = (Label)row.FindControl("Label1"); //ID of the ItemTemplate for my column to which I want ToolTip
            string label2 = label1.Text.Trim();

            if (label2.Length != 0)
            {
                con.Open();
                string str = "SELECT Location_Profile_Tool_Tip FROM Location_Profile_List_ToolTip WHERE Location_Profile_Name='" + label2 + "'";
                SqlCommand cmd = new SqlCommand(str, con);

                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    row.Cells[2].ToolTip = dr[0].ToString().Trim();
                }
                con.Close();
            }
        }
    }
于 2012-09-28T14:28:17.277 に答える