本文介绍了GridViewRow DataItem显示空!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从数据库加载我的网格。但网格没有显示。我彻底调试但它显示GridViewRow获得Null值。我查看了用于加载网格的查询。它在sqlserver中显示特定查询的数据。



我的设计页面:

I m trying to load my grid from database. But the grid is not showing. I debugged thoroughly but It shows that the GridViewRow Is getting Null value. I checked my query that i used for loading the grid. It shows data in sqlserver for the particular query.

My Design Page:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False"  DataSourceID="sql"
                       DataKeyNames="ID" ShowFooter="true"
                       onrowcreated="grid_RowCreated" AllowPaging="false" OnRowCommand="grid_RowCommand" OnRowDataBound="grid_RowDataBound"
                       OnRowUpdating = "grid_RowUpdate" OnRowDeleting="grid_RowDeleting">


                   <Columns>

                   <asp:TemplateField HeaderText="ID" SortExpression="ID">
                       <ItemTemplate>
                           <asp:Label ID="labela" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>


                   <asp:TemplateField HeaderText="Item" SortExpression="Item">
                       <ItemTemplate>
                           <asp:LinkButton ID="label6" runat="server" Text='<%# Bind("Item") %>' CommandName="Item" CommandArgument='<%# Eval("Item","{0}") %>'></asp:LinkButton>
                       </ItemTemplate>

                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="B" SortExpression="B">
                       <EditItemTemplate >
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("B") %>'></asp:TextBox>

                       </EditItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("B","{0:n}") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="E" SortExpression="E">

                       <ItemTemplate>
                           <asp:Label ID="label8" runat="server" Text='<%# Bind("E","{0:n}") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="R" SortExpression="R">
                       <ItemTemplate>
                           <asp:Label ID="label10" runat="server" Text='<%# Bind("R","{0:n}") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>

                    <asp:CommandField  ShowEditButton="True" EditText="Edit" />
                     <asp:ButtonField CommandName="Transfer" Text="Tr" />
                     <asp:CommandField  ShowDeleteButton="True" EditText="Delete" />

               </Columns>
               </asp:GridView>

 <asp:SqlDataSource ID="Sql" runat="server"
           ConnectionString="server=x.y.z;uid=xuser;pwd=ypass;database=x"

           SelectCommand="something"
           UpdateCommand="something1"
           DeleteCommand="somethin2"
           ProviderName="System.Data.SqlClient">

           <SelectParameters>
              <asp:QueryStringParameter  Name="ID" DbType="Int32" QueryStringField="ID"/>
               <asp:QueryStringParameter  Name='No' DbType="Int32" QueryStringField="SF"/>
           </SelectParameters>

       </asp:SqlDataSource>





CS页面:





CS Page:

 protected void Details()
    {
        string strConnection = ConfigurationManager.ConnectionStrings["String"].ConnectionString;
        SqlConnection sqlCon = new SqlConnection(strConnection);

 SqlCommand cmd = new SqlCommand("SELECT  [Name],[FA],[FF],[FO],[FP],FAM FROM [GRANTS_X] WHERE GID=" + Convert.ToInt32(Request.QueryString["GID"]), sqlCon);
     
        SqlDataReader sqlDR;

        try
        {
            sqlCon.Open();
            sqlDR = cmd.ExecuteReader();

            while (sqlDR.Read())
            {
//This portion is for loading some of the level above the grid. which is not our concern coz those level are populating data.
                Name.Text = sqlDR["Name"].ToString();
                FA.Text = sqlDR["FA"].ToString();
                GFP.Text = sqlDR["FF"].ToString() + "-" + sqlDR["FO"].ToString() + "-" + sqlDR["FP"].ToString();

                FAM.Text = string.Format("{0:n}", sqlDR["FAM"]);
            }
            sqlDR.Close();

// Here the  GridViewRow is showing null. Even though my query is fine. and i supposed to get some valuse here. 

            GridViewRow grid= grid.FooterRow;
           

            if (grid! = null)

                 lblExpended.Text = string.Format("{0:n}", Convert.ToDecimal(grid.Cells[3].Text)); 
              
            else
             
lblNoDataExp.Visible = true; 
            
          if (!(lblExpended.Text.Equals("")))
            lblBalAmount.Text = string.Format("{0:n}", (Convert.ToDecimal(FAM.Text) - Convert.ToDecimal(lblExpended.Text)));
            if (!(lblBalAmount.Text.Equals("") & lblEncb.Text.Equals("")))
            LEBalance.Text = string.Format("{0:n}", (Convert.ToDecimal(lba.Text) - Convert.ToDecimal(lblEncb.Text)));

        }
        catch { }
        finally
        {
            sqlCon.Close();
        }
    }





RowDataBound功能:





RowDataBound Function:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
// UT some usertype
            if (Session["UT"] != null)
            {
                string strType = Session["UT"].ToString();
                if (!(strType.Equals("x") || strType.Equals("y") || strType.Equals("z")))
                {
                    LinkButton lbkBtnE = (LinkButton)e.Row.Cells[5].Controls[0];
                    LinkButton lbkBtnT = (LinkButton)e.Row.Cells[6].Controls[0];
                    LinkButton lbkBtnD = (LinkButton)e.Row.Cells[7].Controls[0]; 
                    lbkBtnE.Enabled = false;
                    lbkBtnT.Enabled = false;
                    lbkBtnD.Visible = false;

                }
            }
            Btotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "B"));
            Etotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "E"));
            Rtotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "R"));

            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                 
                    LinkButton button = control as LinkButton;
                    if (button != null && button.CommandName == "Delete")
                        // Add delete confirmation
                        button.OnClientClick = "if (!window.confirm('Are you sure you want to delete this record?')) return false;";
                }
            }
        }
         else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[1].Text = "Totals:";
            // for the Footer, display the running totals
            e.Row.Cells[2].Text = string.Format("{0:n}",Btotal);
            e.Row.Cells[3].Text = string.Format("{0:n}",Etotal);
            e.Row.Cells[4].Text = string.Format("{0:n}",Rtotal);
          

            e.Row.BackColor = System.Drawing.Color.Beige;
            e.Row.Font.Bold = true;
         }

    }







我不知道为什么我在GridViewRow中得到null!任何帮助将不胜感激!




I don't know why i m getting null in GridViewRow! Any help would be appreciated!

推荐答案

GridViewRow grid= grid.FooterRow;   // What is this code doing?


if (grid! = null)



代码以上甚至不应该编译。应该存在名称冲突。


The code above should not even compile. There should be a name conflict.



这篇关于GridViewRow DataItem显示空!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:58