GridViewRow DataItem显示空! [英] GridViewRow DataItem is showing Null!!

查看:73
本文介绍了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!

推荐答案

这是太多的代码供任何人真正深入研究。

不确定你的代码是什么应该是,但如果你想将数据绑定到GridView,有更简单的方法来做到这一点。请参阅 GridView.DataBind Method() [ ^ ]

我检查了那个链接,你会有一个使用 DataAdapter 来填充 DataSet 的例子然后绑定到 GridView



这就是说,这段代码对我来说有点奇怪。

This is too much code for anyone to really dig into.
Not sure what it is your code is supposed to, but if you want to bind data to a GridView there are easier ways to do it. See GridView.DataBind Method ()[^]
I you check that link out you will have an example that is using a DataAdapter to fill a DataSet which is then bound to the GridView.

That said, this code snippet looks a bit strange to me.
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.


当我在您的设计器中看到您的gridview代码时,您将 DataSourceId 属性映射到 SqlDataSource ,并在你的代码后面调用了你的查询,你指向数据库的两种方式,你想要哪一种。



如果你想要 SqlDataSource ,那么在sqldatasource的 selectcommand 属性中传递你的选择查询。



如果你想从服务器端绑定然后删除gridview的DataSourceId属性,然后检查。
As I seen your code in your designer for gridview you mapped the DataSourceId Property to SqlDataSource, and in your code behind you called your query, 2 ways you are pointing your database, which one you want.

If you want SqlDataSource then pass your select query in selectcommand property of sqldatasource.

If you want to bind from server side then remove DataSourceId property of gridview and then check.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆