从网格视图访问数据时出错

时间:2016-11-22 17:32:53

标签: c# asp.net gridview

我得到一个'System.EventArgs'在我的代码中没有包含'Row'错误的定义,我不确定如何解决它。我尝试将EventArgs更改为GridViewEventargs,但这会导致用于向数据库添加数据的按钮出现问题。

以下是我的.cs文件中的代码:

protected void add_Click(object sender, EventArgs e)
{
    string connectionString = WebConfigurationManager.ConnectionStrings["AgileDatabaseConnection"].ConnectionString;

    //set-up connection object called 'myConnection'
    SqlConnection myConnection = new SqlConnection(connectionString);
    //myConnection.ConnectionString is now set to connectionString.

    // open database communication
    myConnection.Open();
    GridView1.Visible = true;
    string IDdata = Session["userID"].ToString();
    string OwnerData = txtprojectOwner.Text;
    string MasterData = txtScrumMaster.Text;
    string ManagerData = txtprojectManager.Text;
    TextBox TextBoxfname = (TextBox)e.Row.FindControl("TextboxID");
    String fname = TextBoxfname.Text;
    string query = "INSERT INTO [ProjectTeam](userID, projectMan, projectOwner, scrumMaster,developer) VALUES (@UserID,@Mas, @Man, @Own, @Mas,@Del)";
    SqlCommand myCommand = new SqlCommand(query, myConnection);

    //create a parameterised object
    myCommand.Parameters.AddWithValue("@Mas", MasterData);
    myCommand.Parameters.AddWithValue("@Own", OwnerData);
    myCommand.Parameters.AddWithValue("@Mas", ManagerData);
    myCommand.Parameters.AddWithValue("@dev", TextBoxfname);
    myCommand.Parameters.AddWithValue("@UserID", IDdata);
}

这是我的网格视图的Asp.NET代码。

<asp:TextBox ID="TextBox1" runat="server" Widthu="284px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" EmptyDataText="Make sure you use correct names and or email!" ForeColor="Red" GridLines="None" AllowPaging="True" AllowSorting="True" Width="814px" DataKeyNames="userID">
    <AlternatingRowStyle BackColor="White" />                       
         <Columns> 
             <asp:HyperLinkField DataNavigateUrlFields="userID" DataTextField="firstName" HeaderText="First Name" SortExpression="firstName" DataNavigateUrlFormatString="UserProfile.aspx?userID={0}" />
             <asp:BoundField DataField="lastName" HeaderText="Last Name" SortExpression="lastName" />
             <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
      </Columns>
</asp:GridView> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AgileDatabaseConnection %>" SelectCommand="SELECT [userID], [firstName], [lastName], [email] FROM [Users] WHERE (([firstName] LIKE '%' + @firstName + '%') OR ([lastName] LIKE '%' + @lastName + '%') OR ([email] LIKE '%' + @email + '%'))">
<SelectParameters>
    <asp:ControlParameter ControlID="TextBox1" Name="firstName" PropertyName="Text" Type="String" />
    <asp:ControlParameter ControlID="TextBox1" Name="lastName" PropertyName="Text" Type="String" />
    <asp:ControlParameter ControlID="TextBox1" Name="email" PropertyName="Text" Type="String" />
    <asp:ControlParameter ControlID="TextBox1" Name="userName" PropertyName="Text" Type="String" />

我只需要从网格视图中传输userName以及来自下拉列表的其他数据,我在页面上的其他地方没有遇到任何问题。任何帮助解决此错误或更好的方法将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

当您尝试访问GridView外部的行时,需要指定行号以及正确的GridView。例如,EventArgsrow事件中只有RowDataBound定义。在你的代码片段中它有Button defenitions。

TextBox TextBoxfname = (TextBox)GridView1.Rows[i].FindControl("TextboxID");

如果您的GridView在TemplateField

中包含TextBox,则此方法有效
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("lastName") %>'></asp:TextBox>
                <asp:DropDownList runat="server" ID="DropDownList1">
                    <asp:ListItem Text="A" Value="a"></asp:ListItem>
                    <asp:ListItem Text="B" Value="b"></asp:ListItem>
                    <asp:ListItem Text="C" Value="c"></asp:ListItem>
                </asp:DropDownList>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("userID") %>' Visible="false"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Save" CommandName="getValuesFromGrid" CommandArgument='<%# Container.DataItemIndex %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

使用OnRowCommand

在GridView中使用按钮代码
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "getValuesFromGrid")
    {
        int rowNumber = Convert.ToInt32(e.CommandArgument);

        TextBox textBox = GridView1.Rows[rowNumber].FindControl("TextBox1") as TextBox;
        DropDownList dropDownList = GridView1.Rows[rowNumber].FindControl("DropDownList1") as DropDownList;
        Label label = GridView1.Rows[rowNumber].FindControl("Label1") as Label;
    }
}

后面的代码循环GridView中的每一行

int rowNumber = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    TextBox textBox = GridView1.Rows[rowNumber].FindControl("TextBox1") as TextBox;
    DropDownList dropDownList = GridView1.Rows[rowNumber].FindControl("DropDownList1") as DropDownList;
    Label label = GridView1.Rows[rowNumber].FindControl("Label1") as Label;

    rowNumber++;
}