使用C#代码进行网格视图数据绑定 [英] grid view databind using c# code

查看:63
本文介绍了使用C#代码进行网格视图数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

here is mine code:

SqlConnection conn = new SqlConnection("Data Source=TWS\\SQLEXPRESS; Database=db_umesh; Integrated Security=True;");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgv();
        }
    }
    private void fillgv()
    {
        string qry = "select * from tbl_course";
        SqlCommand cmd = new SqlCommand(qry,conn);
        try
        {
            cmd.Connection.Open();
            gv.DataSource = cmd.ExecuteReader();
            gv.DataBind();
        }
        catch (Exception ex)
        {
            ltrl_msg.Text = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem. ==ListItemType.Item)
        {
        }
        //if (e.Row.RowType.ToString() == ListItemType.Item.ToString() || e.Row.RowType.ToString() == ListItemType.AlternatingItem.ToString())
        //{
        //    //((HtmlInputHidden)e.Row.FindControl("hid")).Value = DataBinder.Eval(e.Row.RowType, "Course_id").ToString();
        //    ((Literal)e.Row.FindControl("ltrl")).Text = DataBinder.Eval(e.Row.RowType, "Course_name").ToString();
        //}
        
       
    }
}


gridviewex.aspx


gridviewex.aspx

<div>
        <asp:GridView runat="server" ID="gv" AutoGenerateColumns="false" PageSize="5"

            GridLines="None" Width="80%"  align="center" onrowdatabound="gv_RowDataBound" >
                <HeaderStyle BackColor="#84A8C9" />
                <AlternatingRowStyle BackColor="#F4FAFF" />
                <RowStyle BackColor="#DBECFB" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <font color="#F4FAFF"><b>Action</b></font>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox runat="server" ID="chkid" Width="16" />
                            <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="edit" />
                            <asp:Button ID="Button2" runat="server" Text="del" CommandName="del" />
                            <input id="hid" runat="server" type="hidden" />
                        </ItemTemplate>
                        <ItemStyle Height="12%" Width="13%" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <font color="#F4FAFF"><b>Name</b></font>
                        </HeaderTemplate>
                        <ItemTemplate>
                             <font color="#034f94">&nbsp;&nbsp;
                                 <%--<asp:Label ID="lblName" runat="server" Text='<%#Eval("Course_name") %>' />--%>
                                 <asp:Literal ID="ltrl" runat="server"></asp:Literal>
                             </font>
                        </ItemTemplate>
                        <ItemStyle Height="12%" HorizontalAlign="Left" Width="28%" />
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <font color="#F4FAFF"><b>Duration</b></font>
                        </HeaderTemplate>
                        <ItemTemplate>
                             <font color="#034f94">&nbsp;&nbsp;
                                 <asp:Literal ID="lt_fees" runat="server" ></asp:Literal>
                             </font>
                        </ItemTemplate>
                        <ItemStyle Height="12%" HorizontalAlign="Left" Width="28%" />
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    </div>
    <asp:Literal runat="server" ID="ltrl_msg"></asp:Literal>



我有问题如何将其绑定到给定的错误
请帮帮我.
谢谢



i have problem how to bind this its given error
please help me.
thanks

推荐答案

预期的 ^ ].


这篇关于使用C#代码进行网格视图数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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