如何将 DataGridView 控件获取到我的窗体中?

How can I get DataGridView control to my form?

提问人:EdA 提问时间:2/26/2021 最后编辑:EdA 更新时间:3/2/2021 访问量:195

问:

我正在使用 Visual Studio 2010 Professional 开发一个电子商务网站。

我需要一个数据网格,它是未绑定的,我可以手动向其添加数据行。我尝试过 Gridview 控件,但它似乎仅适用于数据绑定行。

我已经搜索了 Internet,似乎 DataGridView 控件将允许我执行此操作。但是,当我将其添加到工具箱时,它显示为灰色,我无法将其添加到我的表单中。

我正在使用 VB,而不是 C#。

你能告诉我我做错了什么吗?任何帮助将不胜感激。

3/2/21 更新 感谢您到目前为止的评论,它们实际上帮助我提出了一个更好的问题。

我很抱歉没有说得更具体。

我在 Web 项目中创建了一个 gridview 控件 ASP.net。 我向 asp 网页添加了一个数据源。 当我在 IDE 中测试数据源时,datacontrol 使用存储过程从 SQL 数据库中提取数据,并显示它。

但是,当我尝试通过在本地计算机上运行项目进行测试时,它不会显示任何数据。

Micrsoft 的帮助文件令人遗憾。他们只给出了 C# 示例。

我确实在网格视图上显示了 5 行,数据通常返回的行数比这多。我假设额外的行将自动添加。这是我的问题吗?

我正在使用绑定数据字段。我应该改用动态字段吗? ASPX 代码如下

<%@ Page Language="vb" AutoEventWireup="true" 

Codebehind="AppProdsSelect.aspx.vb"   %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
            height: 492px;
            width: 1106px;
        }
        {
            width: 200px;
        }
        table
        {
            border: 1px solid #ccc;
        }
        table th
        {
            background-color: #F7F7F7;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border-color: #ccc;
        }
    </style>
</head>
<body>
<form id="form1" runat="server" submitdisabledcontrols="True">
                <asp:Image ID="Image1" runat="server" Height="54px" ImageUrl="Images/CrescentLogo2007.bmp"  Width="175px" />
            &nbsp;&nbsp;&nbsp;&nbsp;

            <asp:Label ID="Label1" runat="server" Font-Names="Arial" ForeColor="Black" 
                Text="Call us at 1-800-560-7867" CssClass="style2"></asp:Label>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
                <br />
                &nbsp;&nbsp;

        
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
    <div>


                <asp:Button ID="ButtonMakes" runat="server" Text="Makes" />
                <asp:Button ID="ButtonModels" runat="server" Text="Models" />
                <asp:Button ID="ButtonYears" runat="server" Text="Years" />
                <asp:Button ID="ButtonEngines" runat="server" Text="Engines" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button1" runat="server" Text="View Shopping Cart" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="Label2" runat="server" Text="Items in Cart:" Width="128px"></asp:Label>
    </div>


                <asp:GridView ID="GridView1" runat="server" Width="1080px" 
                    AutoGenerateColumns="False"
                    ShowHeaderWhenEmpty="True" Height="169px" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:BoundField DataField="Product" HeaderText="Product" 
                            SortExpression="Product" />
                        <asp:BoundField HeaderText="Product Code" DataField="ProductCode" 
                            ReadOnly="True" />
                        <asp:BoundField HeaderText="AppQualifier" DataField="AppQualifier" 
                            SortExpression="AppQualifier" />
                        <asp:DynamicField DataField="FootNote" HeaderText="FootNote" ReadOnly="True" />
                        <asp:BoundField HeaderText="Delivery" DataField="Delivery" />
                        <asp:BoundField HeaderText="ListPrice" DataField="ListPrice" 
                            DataFormatString="&quot;{0:c}&quot;" ReadOnly="True" />
                        <asp:BoundField HeaderText="YourPrice" DataField="YourPrice" 
                            DataFormatString="&quot;{0:c}&quot;" ReadOnly="True" />
                        <asp:ButtonField ButtonType="Button" Text="Add 1" CommandName="cmdAdd1" />
                        <asp:BoundField HeaderText="QtyPurch" DataField="QtyPurch" />
                        <asp:ButtonField ButtonType="Button" Text="Remove 1" CommandName="cmdRemove1" />
                    </Columns>
                </asp:GridView>


                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ABCWebConnectionString %>" SelectCommand="GetApplicationProductsSelected" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:SessionParameter DefaultValue="" Name="WeborderNumber" SessionField="Session(&quot;WebOrderNumber&quot;)" Type="Int64" DbType="Int64" />
                    </SelectParameters>
                </asp:SqlDataSource>


</form>
</body>
</html>

背后的代码是 {

部分公共类 _AppProdsSelect 继承 System.Web.UI.Page

Public Event RowCommand As GridViewCommandEventHandler
'Public Event ShadowsPreRender As EventHandler

Dim bBeenHereAdd1 As Boolean, bBeenHereRemove1 As Boolean, bBeenHereDetails As Boolean, bBeen2cmdCompare0 As Boolean
Dim sProdCode As String, sDelivery As String = ""
Protected WithEvents cmdMake As Global.System.Web.UI.WebControls.Button
Protected WithEvents cmdModel As Global.System.Web.UI.WebControls.Button
Protected WithEvents cmdYear As Global.System.Web.UI.WebControls.Button
Protected WithEvents cmdEngine As Global.System.Web.UI.WebControls.Button
Protected WithEvents GridView1 As Global.System.Web.UI.WebControls.GridView
Protected WithEvents Label2 As Global.System.Web.UI.WebControls.Label
Dim liCartCount As Integer

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim GridView1 As New GridView

    'Dim licount As Int32, sListPrice As String              '   , sYourPrice As String
    If Session("UserID") = "" Then
        Response.Redirect("Default.aspx")
    End If
    Session("LastForm") = "AppProdsSelect.aspx"
    If Not IsPostBack _
    And Not IsNothing(Session("Make")) _
    And Not IsNothing(Session("Model")) _
    And Not IsNothing(Session("Year")) _
    And Not IsNothing(Session("Engine")) Then
        Me.cmdMake.Text = Session("Make").ToString
        Me.cmdModel.Text = Session("Model").ToString
        Me.cmdYear.Text = Session("Year").ToString
        Me.cmdEngine.Text = Session("Engine").ToString
    End If


    'Dim bSuccessful As Boolean
    'Dim conn As New SqlConnection(), myReader As SqlDataReader
    'Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    'Dim SqlCon As New SqlClient.SqlConnection
    'SqlCon.ConnectionString = constr
    'SqlCon.Open()
    'Dim cmd As New SqlClient.SqlCommand
    'Dim sSQl As String
    'sSQl = "SELECT  [ID], [WebOrderNum],[XRefKey],[ProductCodeStocking],[AppQualifier]"
    'sSQl = sSQl & ",[FootNote],[Product],[ListPrice],[WarehouseCodeStocking],[YourPrice]"
    'sSQl = sSQl & ",[QtyPurch],[Delivery],[ChooseMe] ,[ExtAmount],[ApplicationNum],[ImageName]"
    'sSQl = sSQl & ",[Make],[Model],[Year],[Engine]"
    'sSQl = sSQl & " FROM([ABCWeb].[dbo].[AppProdsSelect])"
    'sSQl = sSQl & " Where [WebOrderNum]  " = Session("WebOrderNumber")

    'cmd.Connection = SqlCon
    'cmd.CommandText = sSQl
    'Dim oDataAdapter As New SqlClient.SqlDataAdapter(cmd)
    'Dim ds As New DataSet
    'oDataAdapter.Fill(ds)

    'GridView1.DataBind()
    'GridView1.DataSource = ds
    'GridView1.DataBind()


End Sub



Protected Sub cmdMake_Click(sender As Object, e As System.EventArgs) Handles cmdMake.Click
    Session("Lookup2Show") = "Make"
    Response.Redirect("MakeLookup.aspx")
End Sub

Protected Sub cmdModel_Click(sender As Object, e As System.EventArgs) Handles cmdModel.Click
    Session("Lookup2Show") = "Model"
    Response.Redirect("MakeLookup.aspx")
End Sub

Protected Sub cmdYear_Click(sender As Object, e As System.EventArgs) Handles cmdYear.Click
    Session("Lookup2Show") = "Year"
    Response.Redirect("MakeLookup.aspx", False)
End Sub

Protected Sub cmdEngine_Click(sender As Object, e As System.EventArgs) Handles cmdEngine.Click
    Session("Lookup2Show") = "Engine"
    Response.Redirect("MakeLookup.aspx")
End Sub

End 类

}

.NET vb.net visual-studio-2010 datagridview

评论

1赞 Trevor 2/26/2021
ecommerce website在?您是在做桌面应用程序还是 Web 应用程序?此外,请阅读操作方法页面并阅读如何创建最小、完整和可验证的示例,以改进您的问题并帮助我们理解您的问题。vb.net
2赞 JohnG 2/27/2021
听起来您正在尝试为应用程序类型使用错误的网格控件。此外,无论您使用哪种类型的应用程序,我都很难相信任何网格控件都会......“仅适用于数据绑定行”......我建议您仔细查看可用于项目类型的网格控件。我相信它将允许您在不使用数据源的情况下添加行。
1赞 jmcilhinney 2/27/2021
该控件严格用于 WinForms。如果当前使用的是标准控件,则可能是在创建一个 ASP.NET Web 窗体项目。 不是一种选择。在这种情况下,请解释为什么不能绑定数据?绑定网格时,用户仍可以在 UI 中向网格添加行。我怀疑你绝对可以绑定你的数据,但你只是不知道如何正确地做到这一点。DataGridViewGridViewDataGridView
0赞 EdA 3/2/2021
我添加了更多细节。我希望你能告诉我我做错了什么 - Ed

答: 暂无答案