数据绑定到GridView

时间:2014-09-03 08:25:27

标签: c# gridview datagridview

基本上,我在PageLoad事件上从SQL源将数据绑定到GridView。当我在RowCreated事件期间引用数据时,出现Object reference not set to an instance of an object.错误。

.aspx代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20" Style="table-layout:fixed" AutoGenerateColumns="true" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
                    <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField HeaderText="File">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
.cs事件的

PageLoad代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
        {                
                using (SqlCommand cmd = new SqlCommand())
                {
                    String sql = "select * from dbo.Documents";
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    con.Open();
                    DataSet ds = new DataSet();
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(ds);
                    }
                    DataTable myTable = new DataTable();
                    myTable = ds.Tables[0];
                    this.GridView1.DataSource = myTable;
                    this.GridView1.DataBind();
                }
        if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }  
        }                
            MultiView1.SetActiveView(vHome);
            btnBacktoHome.Visible = false;
            lblStatus.Visible = false;
        }
    }
.cs事件的

RowCreated代码:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        DataTable myGridTable = null;
        TableCell tblCell1;
        myGridTable = (DataTable)((GridView)sender).DataSource;
        if (myGridTable != null)
        {
            tblCell1 = e.Row.Cells[myGridTable.Columns["File"].Ordinal];
            tblCell1.BackColor = System.Drawing.Color.Red;
        } 
    }
Object reference not set to an instance of an object.事件的tblCell1 = e.Row.Cells[myGridTable.Columns["File"].Ordinal];行引发了

RowCreated错误。所以基本上,当我在this.GridView1.DataSource = myTable;事件到(RowCreated)中引用GridView1数据源(tblCell1 = e.Row.Cells[myGridTable.Columns["File"].Ordinal];)时,它告诉我this.GridView1.DataSource = myTable;中没有数据限制。当我在PageLoad()上绑定来自SQL源的数据时,怎么会这样呢?有谁能看到这个问题?

0 个答案:

没有答案