获取选定行的DataGridView不显示文本在文本框中。

huangapple go评论53阅读模式
英文:

get selected row datagridview didn't show text in textbox

问题

如何在用户输入数据时,在选定的行中显示文本框中的文本。当我单击获取选定数据时,仅在我的数量上显示空白。

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();
}
英文:

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();
        }

答案1

得分: 1

这是你要翻译的内容:

"简单的规则是记住以下内容:

对于数据绑定(内置)GridView控件,您使用cells[]集合。

对于模板控件,您使用row.findcontrol。

实际上,您对于复选框“sort of”做到了这一点。

所以,这将起作用:

{
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();
}

请注意,我甚至抛弃了.cells[0],因为我们只关心问题中所述的模板控件在给定行的“某处”。

我还要指出,我假设这只是一个学习示例,因为从理论上讲,我们正在从数据库中填充GridView,并且还希望保存/更新结果值。

如果是这样的情况,那么我们不需要“从头开始”创建我们选择的全新数据表,而是实际上只获取/抓取主键值和数量,然后将其写回到数据库。

因此,一般规则是,不要试图将GridView用作“数据存储”系统,而仅用它来显示数据,允许用户选择行,并获取数量值。但是,一般情况下,我们不需要也不需要获取零件号信息,因为它将由给定行的主键值标识。

不管是否“远离”创建该表格并使用/拥有某个“myorders”表,上述方法都应该起作用。"

英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 12:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定