I have a gridview that was bounded to a drop down list. When a value is selected from a drop down list, the grid view will display only data that related to the value that is selected. But, data in the gridview cannot be edited.
My problem is, i want to make the data in the gridview as editable. When i set the gridview to Enable Editing and debug it, an error was occur.
Please someone help me on how to make it???

Here is my code:

public partial class ViewPipelineByCategory : System.Web.UI.Page
    {
        DBConn myDB = new DBConn();
        SqlConnection SQLConn = new SqlConnection();
        DataSet pipelineDataSet = new DataSet();
        SqlDataAdapter pipelineSqlDataAdapter = new SqlDataAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = SqlDataSource1;

                GridView1.DataBind();

                CreateDataSet();
                SqlCommand pipelineSqlCommand =
                new SqlCommand("Select DISTINCT category_pipeline FROM Project_Pipeline", SQLConn);
                SqlDataReader pipelineSqlDataReader;
                SQLConn.Open();
                pipelineSqlDataReader = pipelineSqlCommand.ExecuteReader();
                categoryList.DataSource = pipelineSqlDataReader;
                categoryList.DataTextField = "category_pipeline";
                categoryList.DataBind();
                pipelineSqlDataReader.Close();
                SQLConn.Close();
            }

        }

        private void CreateDataSet()
        {
            SQLConn.ConnectionString =
            SqlDataSource1.ConnectionString;
            pipelineSqlDataAdapter.SelectCommand = new
            SqlCommand(SqlDataSource1.SelectCommand, SQLConn);
            pipelineSqlDataAdapter.Fill(pipelineDataSet);
        }

        protected void categoryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateDataSet();
            string categoryName = categoryList.SelectedItem.Value.Trim();
            DataView pipelineDataView = new DataView(pipelineDataSet.Tables[0]);
            pipelineDataView.RowFilter = "category_pipeline = '" + categoryName + "' ";
            GridView1.DataSource = pipelineDataView;
            GridView1.DataBind();
        }
   }

Here is the grid view code:

<tr>
                            <td>
                                &nbsp;</td>
                            <td class="style6" colspan="2">
                                <br />
                                <b>View By: </b>&nbsp;<br />
&nbsp;<br />
                                Category: 
                                <asp:DropDownList ID="categoryList" runat="server" AutoPostBack="True" 
                                    onselectedindexchanged="categoryList_SelectedIndexChanged">
                                </asp:DropDownList>
                                <br />
                                <br />
                                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
                                    CellPadding="3" DataKeyNames="pipeline_id" 
                                    GridLines="Horizontal">
                                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="COMPANY NAME" SortExpression="company_name">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList2" runat="server" 
                                                    DataSourceID="SqlDataSource3" DataTextField="company_name" 
                                                    DataValueField="company_name" SelectedValue='<%# Bind("company_name") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("company_name") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="project_name" HeaderText="PIPELINE PROJECTS" 
                                            SortExpression="project_name" />
                                        <asp:BoundField DataField="description" HeaderText="DESCRIPTION" 
                                            SortExpression="description" />
                                        <asp:BoundField DataField="project_revenue" HeaderText="PROJECTED REVENUE (RM)" 
                                            SortExpression="project_revenue" />
                                        <asp:BoundField DataField="cost" HeaderText="GROSS PROFIT (RM)" 
                                            SortExpression="cost" />
                                        <asp:TemplateField HeaderText="STATUS" SortExpression="status_pipeline">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList1" runat="server" 
                                                    DataSourceID="SqlDataSource2" DataTextField="status_pipeline" 
                                                    DataValueField="status_pipeline" SelectedValue='<%# Bind("status_pipeline") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("status_pipeline") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="CATEGORY" SortExpression="category_pipeline">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList3" runat="server" 
                                                    DataSourceID="SqlDataSource4" DataTextField="category_pipeline" 
                                                    DataValueField="category_pipeline" 
                                                    SelectedValue='<%# Bind("category_pipeline") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("category_pipeline") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="BDE" SortExpression="bde_name">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList4" runat="server" 
                                                    DataSourceID="SqlDataSource5" DataTextField="bde_name" 
                                                    DataValueField="bde_name" SelectedValue='<%# Bind("bde_name") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("bde_name") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="deadline" HeaderText="DEADLINE" 
                                            SortExpression="deadline" />
                                        <asp:BoundField DataField="remark" HeaderText="REMARK" 
                                            SortExpression="remark" />
                                        <asp:BoundField DataField="pipeline_id" HeaderText="pipeline_id" 
                                            InsertVisible="False" ReadOnly="True" SortExpression="pipeline_id" 
                                            Visible="False" />
                                    </Columns>
                                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <AlternatingRowStyle BackColor="#F7F7F7" />
                                </asp:GridView>
                                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:BDASConnectionString %>" 
                                    SelectCommand="SELECT * FROM [Project_Pipeline]">
                                </asp:SqlDataSource>
                                <br />
                            </td>
                            <td>
                                &nbsp;</td>
                        </tr>

Hope that someone can help me...

Recommended Answers

All 3 Replies

hey what's the error ?

also suppose you will receive 5 rows for a selected value from the Dropdown, then in the case do you want to show gridview editable for all the five rows ?

you can show your gridview editable by default with the help of TemplateField and in ItemTemplate use textboxes etc..

========================================================================================

I have a gridview that was bounded to a drop down list. When a value is selected from a drop down list, the grid view will display only data that related to the value that is selected. But, data in the gridview cannot be edited.
My problem is, i want to make the data in the gridview as editable. When i set the gridview to Enable Editing and debug it, an error was occur.
Please someone help me on how to make it???

Here is my code:

public partial class ViewPipelineByCategory : System.Web.UI.Page
    {
        DBConn myDB = new DBConn();
        SqlConnection SQLConn = new SqlConnection();
        DataSet pipelineDataSet = new DataSet();
        SqlDataAdapter pipelineSqlDataAdapter = new SqlDataAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridView1.DataSource = SqlDataSource1;

                GridView1.DataBind();

                CreateDataSet();
                SqlCommand pipelineSqlCommand =
                new SqlCommand("Select DISTINCT category_pipeline FROM Project_Pipeline", SQLConn);
                SqlDataReader pipelineSqlDataReader;
                SQLConn.Open();
                pipelineSqlDataReader = pipelineSqlCommand.ExecuteReader();
                categoryList.DataSource = pipelineSqlDataReader;
                categoryList.DataTextField = "category_pipeline";
                categoryList.DataBind();
                pipelineSqlDataReader.Close();
                SQLConn.Close();
            }

        }

        private void CreateDataSet()
        {
            SQLConn.ConnectionString =
            SqlDataSource1.ConnectionString;
            pipelineSqlDataAdapter.SelectCommand = new
            SqlCommand(SqlDataSource1.SelectCommand, SQLConn);
            pipelineSqlDataAdapter.Fill(pipelineDataSet);
        }

        protected void categoryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateDataSet();
            string categoryName = categoryList.SelectedItem.Value.Trim();
            DataView pipelineDataView = new DataView(pipelineDataSet.Tables[0]);
            pipelineDataView.RowFilter = "category_pipeline = '" + categoryName + "' ";
            GridView1.DataSource = pipelineDataView;
            GridView1.DataBind();
        }
   }

Here is the grid view code:

<tr>
                            <td>
                                &nbsp;</td>
                            <td class="style6" colspan="2">
                                <br />
                                <b>View By: </b>&nbsp;<br />
&nbsp;<br />
                                Category: 
                                <asp:DropDownList ID="categoryList" runat="server" AutoPostBack="True" 
                                    onselectedindexchanged="categoryList_SelectedIndexChanged">
                                </asp:DropDownList>
                                <br />
                                <br />
                                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
                                    CellPadding="3" DataKeyNames="pipeline_id" 
                                    GridLines="Horizontal">
                                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="COMPANY NAME" SortExpression="company_name">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList2" runat="server" 
                                                    DataSourceID="SqlDataSource3" DataTextField="company_name" 
                                                    DataValueField="company_name" SelectedValue='<%# Bind("company_name") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("company_name") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="project_name" HeaderText="PIPELINE PROJECTS" 
                                            SortExpression="project_name" />
                                        <asp:BoundField DataField="description" HeaderText="DESCRIPTION" 
                                            SortExpression="description" />
                                        <asp:BoundField DataField="project_revenue" HeaderText="PROJECTED REVENUE (RM)" 
                                            SortExpression="project_revenue" />
                                        <asp:BoundField DataField="cost" HeaderText="GROSS PROFIT (RM)" 
                                            SortExpression="cost" />
                                        <asp:TemplateField HeaderText="STATUS" SortExpression="status_pipeline">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList1" runat="server" 
                                                    DataSourceID="SqlDataSource2" DataTextField="status_pipeline" 
                                                    DataValueField="status_pipeline" SelectedValue='<%# Bind("status_pipeline") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("status_pipeline") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="CATEGORY" SortExpression="category_pipeline">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList3" runat="server" 
                                                    DataSourceID="SqlDataSource4" DataTextField="category_pipeline" 
                                                    DataValueField="category_pipeline" 
                                                    SelectedValue='<%# Bind("category_pipeline") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("category_pipeline") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="BDE" SortExpression="bde_name">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownList4" runat="server" 
                                                    DataSourceID="SqlDataSource5" DataTextField="bde_name" 
                                                    DataValueField="bde_name" SelectedValue='<%# Bind("bde_name") %>'>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("bde_name") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="deadline" HeaderText="DEADLINE" 
                                            SortExpression="deadline" />
                                        <asp:BoundField DataField="remark" HeaderText="REMARK" 
                                            SortExpression="remark" />
                                        <asp:BoundField DataField="pipeline_id" HeaderText="pipeline_id" 
                                            InsertVisible="False" ReadOnly="True" SortExpression="pipeline_id" 
                                            Visible="False" />
                                    </Columns>
                                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <AlternatingRowStyle BackColor="#F7F7F7" />
                                </asp:GridView>
                                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:BDASConnectionString %>" 
                                    SelectCommand="SELECT * FROM [Project_Pipeline]">
                                </asp:SqlDataSource>
                                <br />
                            </td>
                            <td>
                                &nbsp;</td>
                        </tr>

Hope that someone can help me...

Try This..

it shows how to edit and update..!

hey what's the error ?

also suppose you will receive 5 rows for a selected value from the Dropdown, then in the case do you want to show gridview editable for all the five rows ?

you can show your gridview editable by default with the help of TemplateField and in ItemTemplate use textboxes etc..

========================================================================================

When i set the gridview to Enable editing and then i do the code to bound the grid view to a drop down list, an error has occur when i debug the code.

The error is:
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.

That error occur at GridView1.DataBind();

And then, when i delete the DataSourceID (SqlDataSource1) but still maintaining the gridview to Enable Editing, then i debug the code, it can display interface with the gridview. But when i click the "Edit" button, An error occur.

The error is:
The GridView 'GridView1' fired event RowEditing which wasn't handled.

Then, when i set back the grid view to Enable Editing as False, it works without error. So, I don't know how to make the gridview to Enable Editing.

Can someone help me???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.