Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
I have been asked infinite number of times that how do I access a value of a checkbox that is present inside the datagrid control. When I finish answering the question a new question pops up "How do I access dropdownlist value present inside datagrid control?". Instead of answering these questions separately I wrote an article which deals with accessing values of different controls inside datagrid. Following is a code snippet from my article:
foreach(DataGridItem dgi in myDataGrid.Items)

{

TextBox myTextBox = (TextBox) (dgi.Cells[0].Controls[1]);

ListBox myListBox = (ListBox) (dgi.Cells[1].Controls[1]);

DropDownList myList = (DropDownList) (dgi.Cells[2].Controls[1]);

CheckBox myCheckBox = (CheckBox) (dgi.Cells[3].Controls[1]);

str.Append(myTextBox.Text);

string a = myListBox.SelectedValue;

if(a != null && a != "")

{

str.Append(myListBox.SelectedItem.Text);

}

str.Append(myList.SelectedItem.Text);

str.Append(myCheckBox.Checked);

}


You can check out the complete article at: 

 http://www.codersource.net/asp_net_accessing_different_values_datagrid.aspx 

Posted on Wednesday, June 22, 2005 4:44 PM | Back to top


Comments on this post: Accessing values of different controls inside the Datagrid control

# re: Accessing values of different controls inside the Datagrid control
Requesting Gravatar...
I'm not familiar with the syntax of C#..what does the (CheckBox) do in the (CheckBox) (dgi.Cells[3].Controls[1])?
Left by Viggy on Jul 13, 2005 10:15 AM

# re: Accessing values of different controls inside the Datagrid control
Requesting Gravatar...
Basically we are converting what ever is in Cell[3] of the Datagrid to Checkbox control.

(Checkbox) (dgi.Cells[3].Controls[1])

Left by AzamSharp on Jul 13, 2005 12:47 PM

# re: Accessing values of different controls inside the Datagrid control
Requesting Gravatar...
Check out my article at

www.gridviewguy.com

Left by AzamSharp on Jul 13, 2005 12:47 PM

# re: Accessing values of different controls inside the Datagrid control
Requesting Gravatar...
IF i do not have any control, and i am binded the data , then how to get the item value , in run time,
<asp:DataGrid id="datagrid1" AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Job desc" ItemStyle-Width="400">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "job_desc")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Left by Pradeep K v on Aug 16, 2005 9:44 PM

# re: Accessing values of different controls inside the Datagrid control
Requesting Gravatar...
Not sure if I got the question. But you can assign the DataSet to the Datagrid control and bind the values.
Left by Azamsharp on Aug 17, 2005 5:48 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net