I am using mysql and asp.net with c#. I have a grid view which will display dynamically selected table data. I am able to display the data of selected table. In the first column i have added a check box and a Button outside Grid view. When user selects Check box and clicks on button, the selected rows must turn into text boxes. I am able to find the seleted check box, but i'm unable to convert the cells into text boxes. Here's my code:
我用c#使用mysql和asp.net。我有一个网格视图,它将显示动态选择的表数据。我能够显示所选表格的数据。在第一列中,我添加了一个复选框和一个Grid视图外的Button。当用户选中“复选框”并单击“按钮”时,所选行必须变为文本框。我能够找到选中的复选框,但我无法将单元格转换为文本框。这是我的代码:
int n = GridView1.HeaderRow.Cells.Count;
for( int i=0; i < GridView1.Rows.Count;i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
{
for( int j=0;j<n;j++)
{
TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
}
}
}
At this line: TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
i get a warning : cannot convert 'System.Web.UI.Controls.TableCell' to type 'System.Web.UI.Controls.TextBox
I am unable to resolve this. Please help. Thank you
在这一行:TextBox txt =((TextBox)GridView1.Rows [i] .Cells [j])。Text;我得到一个警告:无法将'System.Web.UI.Controls.TableCell'转换为'System.Web.UI.Controls.TextBox'我无法解决此问题。请帮忙。谢谢
2 个解决方案
#1
2
Try this.
尝试这个。
You can remove one or few lines based on your hands-on with C#.
您可以根据自己动手使用C#删除一行或几行。
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
概念是,您应该创建一个TextBox,分配文本框的单元格文本,然后将新创建的文本框添加到特定行的Grid Cell的子控件。
Mark this solution if you found useful.
如果您觉得有用,请标记此解决方案
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
如果要避免TextBox出现在CheckBox列中,请使用1而不是0初始化循环变量j。
for( int j=1;j<n;j++)
#2
0
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
您的方法调用是查找单元格。文本框是单元格中包含的控件。尝试这样的事情:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl
method is documented here.
FindControl方法在此处记录。
Also take @Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.
另请参考@ Bartdude关于使用可编辑网格视图的建议。如果一个人的工作至少和你想要手动滚动的一样好,那么值得花时间学习如何使用它。
#1
2
Try this.
尝试这个。
You can remove one or few lines based on your hands-on with C#.
您可以根据自己动手使用C#删除一行或几行。
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
概念是,您应该创建一个TextBox,分配文本框的单元格文本,然后将新创建的文本框添加到特定行的Grid Cell的子控件。
Mark this solution if you found useful.
如果您觉得有用,请标记此解决方案
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
如果要避免TextBox出现在CheckBox列中,请使用1而不是0初始化循环变量j。
for( int j=1;j<n;j++)
#2
0
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
您的方法调用是查找单元格。文本框是单元格中包含的控件。尝试这样的事情:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl
method is documented here.
FindControl方法在此处记录。
Also take @Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.
另请参考@ Bartdude关于使用可编辑网格视图的建议。如果一个人的工作至少和你想要手动滚动的一样好,那么值得花时间学习如何使用它。