【发布时间】:2009-03-10 11:22:32
【问题描述】:
我正在用一些值填充一个 C# DataGrid,当我单击它时我想从一个单元格中检索一个值。我该如何使用 .NET 1.1 Framework 来做到这一点?
datagridview1 在 .net1.1 中不可用
仅适用于 Windows 应用程序
【问题讨论】:
-
你在单元格中点击什么...是链接按钮、文本还是按钮?
我正在用一些值填充一个 C# DataGrid,当我单击它时我想从一个单元格中检索一个值。我该如何使用 .NET 1.1 Framework 来做到这一点?
datagridview1 在 .net1.1 中不可用
仅适用于 Windows 应用程序
【问题讨论】:
如果您谈论的是 web/ASP.Net 1.1 DataGrid:
myDataGrid.Items 获取一行row.Cells[x] 获取该行中的列(TextBox)cell.FindControl("TextBox1") 获取单元格内的控件欲了解更多信息,请参阅Accessing a Control’s Value from DataGrid in ASP.NET
【讨论】:
做以下工作:
string strCell = (string)dataGridView1.CurrentCell.Value;
【讨论】:
这是http://www.jigar.net/articles/viewhtmlcontent4.aspx 的摘录,也可以在 Eric Lathrop 的回答中找到链接。这涵盖了大多数常见场景。
请记住,在以下示例中,dataGridItem 是 MyDataGrid.Items[x] 的别名,其中 x 是(行)索引。这是因为以下示例使用了 foreach 循环,所以如果略读请记住这一点。
遍历 DataGrid 的行
我们必须遍历 DataGrid 的行来获取该行中控件的值,所以 让我们先这样做。 DataGrid 控件有一个名为 Items 的属性, 它是对象 DataGridItem 的集合,表示 DataGrid 控件中的单个项目,我们可以使用此属性来 按照以下六个步骤遍历 DataGrid 行。foreach(DataGridItem dataGridItem in MyDataGrid.Items){ }从 DataGrid 中的绑定列中获取值
我们的第一列是绑定列,我们需要写入一个值 在那一栏中。 DataGridItem 有一个名为 Cells 的属性,它是 表示单元格的 TableCell 对象的集合 行。 TableCell 的 Text 属性为我们提供了写入的值 那个特定的细胞。
//Get name from cell[0] String Name = dataGridItem.Cells[0].Text;在 DataGrid 中获取 TextBox 控件的值 现在我们的第二列包含一个 TextBox 控件,我们需要获取一个 Text 属性 那个物体的。我们使用 DataGridItem 的 FindControl 方法来 获取 TextBox 的参考。
//Get text from textbox in cell[1] String Age = ((TextBox)dataGridItem.FindControl("AgeField")).Text;从 DataGrid 中的 CheckBox 控件中获取值
我们的第三栏 在 DataGrid 中包含一个 CheckBox Web 控件,我们需要检查它是否 该控件的 Checked 属性是 true 还是 false。
//Get Checked property of Checkbox control bool IsGraduate = ((CheckBox)dataGridItem.FindControl ("IsGraduateField")).Checked;从 DataGrid 中的 CheckBoxList Web 控件中获取值
这个案例 与前一个不同,因为 CheckBoxList 可能返回更多 然后选择一个值。我们必须遍历 CheckBoxList items 以检查用户是否选择了特定项目。
// Get Values from CheckBoxList String Skills = ""; foreach(ListItem item in ((CheckBoxList)dataGridItem.FindControl("CheckBoxList1")).Items) { if (item.Selected){ Skills += item.Value + ","; } } Skills = Skills.TrimEnd(',');从 DataGrid 中的 RadioButtonList Web 控件中获取值
我们使用 DataGridItem 的 FindControl 方法来获取 RadioButtonList 和 SelectedItem 属性 RadioButtonList 从 RadioButtonList 中获取所选项目。
//Get RadioButtonList Selected text String Experience = ((RadioButtonList)dataGridItem.FindControl("RadioButtonList1")) .SelectedItem.Text;从 DataGrid 中的 DropDownList Web 控件中获取值
这类似于 RadioButtonList。我用这个控件只是为了展示 它的工作方式与任何其他 ListControls 相同。同样,你 可以像使用 CheckBoxList 一样使用 ListBox Web 控件 控制。
//Get DropDownList Selected text String Degree = ((DropDownList)dataGridItem. FindControl("DropDownList1")).SelectedItem.Text;
【讨论】: