如何从DataGridView和Display中检索数据

本文关键字:检索 数据 Display DataGridView | 更新日期: 2023-09-27 18:18:36

dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);

我有一个DataGrid位于一个表单和按钮将填充这个数据的数据网格。如果选择了选定行或单元格,如何在文本框或其他数据网格中显示该数据?最好是一个文本框

如何从DataGridView和Display中检索数据

试试这个代码示例…它适合我

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int index = GridView1.SelectedIndex;
                this.pcode = GridView1.Rows[index].Cells[0].Text;
            }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // Get reference to button field in the gridview.  
                    LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
                    string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
                    e.Row.Style["cursor"] = "hand";
                    e.Row.Attributes["onclick"] = _jsSingle;
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
                    {
                        Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
                    }
                }
            }
            base.Render(writer);
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "ColumnClick")
            {
                foreach (GridViewRow r in GridView1.Rows)
                {
                    if (r.RowType == DataControlRowType.DataRow)
                    {
                        for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
                        {
                            r.Cells[columnIndex].Attributes["style"] += "background-color:White;";
                        }
                    }
                }
                selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
                // int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
                GridView1.Rows[selectedRowIndex].Cells[1].Attributes["style"] += "background-color:Red;";
                TextBox2.Text = GridView1.Rows[selectedRowIndex].Cells[2].Text; 
            }
        }