Search code examples
checkboxwebforms

get selected row datagridview didn't show text in textbox


How to show text in textbox in selected rows when user enter data. It's only show blank for my quantity when I click on get selected data

protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item"), new DataColumn("Number"), new DataColumn("Name"), new DataColumn("Quantity"), });
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string item = row.Cells[1].Text;
                        string number = row.Cells[2].Text;
                        string name = row.Cells[3].Text;
                        string quantity = row.Cells[4].Text;
                        dt.Rows.Add(item, number,name,quantity);
                    }
                }
            }
            gvSelected.DataSource = dt;
            gvSelected.DataBind();
        }

Solution

  • Well, the simple rule to remember is this:

    For databound (built in) GV controls, you use cells[] collection.

    For templated controls you use row.findcontrol.

    In fact, you "sort of" did this for the check box.

    So, then this would work:

    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item"), new DataColumn("Number"), new DataColumn("Name"), new DataColumn("Quantity"), });
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.FindControl("chkRow") as CheckBox);
                if (chkRow.Checked)
                {
                    string item = row.Cells[1].Text;
                    string number = row.Cells[2].Text;
                    string name = row.Cells[3].Text;
                    string quantity = (row.FindControl("txtQty") as TextBox).Text;
                    dt.Rows.Add(item, number, name, quantity);
                }
            }
        }
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
    }
    

    Note how I even dumped the .cells[0], since we ONLY care that the templated control in question is "some place" in that given row.

    I will also point out that I assume this is just a learning example, since in theory, we are filling the Gv with data from a database, and we are ALSO looking to save/update the resulting values.

    And that being the case, then we would not from "scratch" create a whole new data table of our selection's, but in fact get/grab ONLY the pk values, and the Qty, and then write that back to the database.

    So, as a general rule, don't try and use the GV as a "data store" system, but only that of a system to display data, allow the user to select rows, and say grab the qty values. But, we as a general rule would not need, nor require to say pull in the part number information, since that would be identified by the given row PK value.

    Regardless of "moving away" from creating that table, and using/having some "myorders" table, the above should work.