Jump to content

How To Set The Index On Datagrid?


joecool2005

Recommended Posts

Hi,I have a datagrid and I would like to change a value on a specific cell.But to do that I need to specify the index of the row.I did this and it does not work.

Dim dgi As DataGridItemdgi.ItemIndex = 1	  'this line does not workdgi.Cells(2).Text = "my new value"

Can you help me?Thx

Link to comment
Share on other sites

Here is my problem.I have a datagrid that has check box on each row. Any time the check box is checked, a text box appears in the last column and user can add any values on that text box.When user clicks on other check box, the page post back and the value entered in the text box is reseted.I want to keep all the values that has been entered by the user.And don't know how.This is the datagrid

<asp:datagrid id="grdEmployees" runat="server" BorderStyle="None" BorderWidth="1px" BackColor="White" BorderColor="#CCCCCC" CellPadding="2" GridLines="Both" ForeColor="Black" Font-Size="8pt"><SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="LightCoral"></SelectedItemStyle><Columns><asp:TemplateColumn><ItemTemplate><asp:CheckBox id="chkSelect" runat="server" OnCheckedChanged="grdEmployees_CheckedChanged" AutoPostBack="true"></asp:CheckBox></ItemTemplate></asp:TemplateColumn></Columns><PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="White"></PagerStyle></asp:datagrid>

this is the functions

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load		If Not Me.IsPostBack Then			Dim dt As DataTable			dt = New DataTable			Dim dr As DataRow			dt.Columns.Add(New DataColumn("<font face=""Verdana"" size=""2"">col 1</font>", GetType(String)))			dt.Columns.Add(New DataColumn("<font face=""Verdana"" size=""2""><center>col 2</center></font>", GetType(String)))			dt.Columns.Add(New DataColumn("<font face=""Verdana"" size=""2""><center>col 3</center></font>", GetType(String)))			dt.Columns.Add(New DataColumn("<font face=""Verdana"" size=""2""><center>col 4</center></font>"))			dr = dt.NewRow()			dr(0) = "<font face=""Verdana"" size=""2""><center>0</center></font>"			dr(1) = "<font face=""Verdana"" size=""2""><center>1</center></font>"			dr(2) = "<font face=""Verdana"" size=""2""><center>2</center></font>"			dt.Rows.Add(dr)			dr = dt.NewRow()			dr(0) = "<font face=""Verdana"" size=""2""><center>0</center></font>"			dr(1) = "<font face=""Verdana"" size=""2""><center>1</center></font>"			dr(2) = "<font face=""Verdana"" size=""2""><center>2</center></font>"			dt.Rows.Add(dr)			dr = dt.NewRow()			dr(0) = "<font face=""Verdana"" size=""2""><center>0</center></font>"			dr(1) = "<font face=""Verdana"" size=""2""><center>1</center></font>"			dr(2) = "<font face=""Verdana"" size=""2""><center>2</center></font>"			dt.Rows.Add(dr)			grdEmployees.DataSource = New DataView(dt)			grdEmployees.DataBind()		End If	End Sub	Public Sub grdEmployees_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)		Dim chkTemp As CheckBox = CType(sender, CheckBox)		Dim dgi As DataGridItem		dgi = CType(chkTemp.Parent.Parent, DataGridItem)		If (chkTemp.Checked) Then			dgi.BackColor = grdEmployees.SelectedItemStyle.BackColor			dgi.ForeColor = grdEmployees.SelectedItemStyle.ForeColor			dgi.Cells(4).Text = "<input type=text />"		Else			dgi.BackColor = grdEmployees.ItemStyle.BackColor			dgi.ForeColor = grdEmployees.ItemStyle.ForeColor			dgi.Cells(4).Text = ""		End If	End Sub

Thx for your help

Link to comment
Share on other sites

One of your problems is here:

dgi.Cells(4).Text = "<input type=text />"

While that will, as you've seen, add a text box to the page in the location that you are expecting, your code-behind cannot have access to that control.If you add a textbox to a web page in the aspx, you do it one of the following ways:

<input type="text" id="myText" runat="server" /><asp:TextBox id="myText2" runat="server" />

The key there is that the control has to run at server in order for the code-behind to have access to it.Since you're doing this dynamically, this approach isn't going to work for you. However, if you'd set it up so that those text boxes are already on the page, just styled with "display:none", you can add an onclick to the checkbox to make the boxes visible again. This way, the boxes will be there on the postback and you'll have all the values every time the page posts back to the server.

Link to comment
Share on other sites

Thanks for your answer.I still don't know how.I added this on Sub Page_Load for each row.

dr(3) = "<input type=text style=""display:none"" runat=""server""/>"

And on rdEmployees_CheckedChanged function I added this

If (chkTemp.Checked) Then  ...  dgi.Cells(4).Text = Replace(dgi.Cells(4).Text, "none", "inline")Else  ...  dgi.Cells(4).Text = Replace(dgi.Cells(4).Text, "inline", "none")End If

And I still losing the data.What should I do?Thanks

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...