1

我正在绑定gridview并在页脚中显示它们相应的总数,没有任何错误。现在我想添加额外的运行,如下图所示(这是完全离散的数据行,它不属于数据集)。我想实现如下图所示的功能。

在此处输入图像描述

这是我的代码:

<asp:GridView ID="gvFirstInningBatting" runat="server" AllowSorting="True" AutoGenerateColumns="false"
        GridLines="None" AllowPaging="false" CellPadding="4" CssClass="GridViewStyle" OnRowDataBound="gvFirstInningBatting_RowDataBound"
        ShowFooter="true" ShowHeader="true" Width="100%">
        <Columns>
            <asp:BoundField DataField="P_PlayerPopulerName" HeaderText="Batting" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Left" ItemStyle-Width="20%" />
            <asp:BoundField DataField="SBTS_PlayerStatus" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Left" ItemStyle-Width="20%" />
            <asp:BoundField DataField="SBTS_RunsScored" HeaderText="R" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
            <asp:BoundField DataField="SBTS_MinutesBatted" HeaderText="M" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
            <asp:BoundField DataField="SBTS_BallsPlayed" HeaderText="B" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
            <asp:BoundField DataField="SBTS_Fours" HeaderText="4s" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
            <asp:BoundField DataField="SBTS_Sixes" HeaderText="6s" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
            <asp:BoundField DataField="SBTS_StrikeRate" HeaderText="SR" HeaderStyle-HorizontalAlign="Center"
                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%" />
        </Columns>
    </asp:GridView>

在 C# 中

protected void BindGridView(int scheduleId)
        {
            SqlConnection dBConnection = null;
            try
            {
                dBConnection = new SqlConnection();
                dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
                SqlDataAdapter dataAdapter = new SqlDataAdapter();

                SqlCommand cmd = new SqlCommand("SP_NAME", dBConnection);
                cmd.CommandType = CommandType.StoredProcedure;
                dBConnection.Open();

                DataSet ds = new DataSet();
                DataTable dtBatting = new DataTable();
                dataAdapter.SelectCommand = cmd;
                dataAdapter.Fill(ds);

                int gvBattingCount = Convert.ToInt32(cmd.Parameters["@BattingRowsCount"].Value);

                for (int i = 0; i < gvBattingCount; i++)
                {
                    if (ds.Tables[i].Rows.Count > 0)
                    {
                        if (i == 0)
                        {
                            DataRow dr1st;

                            ds.Tables[i].DefaultView.RowFilter = "SBTS_PlayerIsBatted = 1";
                            dtBatting = ds.Tables[i].DefaultView.ToTable();

                            dr1st = dtBatting.NewRow();
                            dr1st["P_PlayerPopulerName"] = " ";
                            dr1st["SBTS_RunsScored"] = 36;

                            dr1st["SBTS_PlayerStatus"] = "Extras";
                            dr1st["SBTS_MinutesBatted"] = 0;
                            dr1st["SBTS_BallsPlayed"] = 0;
                            dr1st["SBTS_Fours"] = 0;
                            dr1st["SBTS_Sixes"] = 0;
                            dr1st["SBTS_StrikeRate"] = 0;
                            dtBatting.Rows.Add(dr1st);

                            gvFirstInningBatting.DataSource = dtBatting;
                            gvFirstInningBatting.DataBind();
                        }
                    }
                }
           }

如果我使用,网格正在工作,dr1st["SBTS_Sixes"] = 0;但它在行中显示 0,如果我使用,dr1st["SBTS_Sixes"] = DBNull.Value;那么它会抛出错误

对象不能从 DBNull 转换为其他类型。

我该怎么办,请帮帮我。

4

0 回答 0