Finding Dynamically Created DataGrid Controls

G

Guest

Hi Guys

I have a datagrid that are databound at runtime and columns are added automatically to it from the dataset. OnEdit Command renders all fields as editable, my problem is I need to specify in some way which columns are not editable, I have the columns I would like to lock for editing, but how do I tell the datagrid this ? My columns are dynamically added to the grid at runtime, because the dataset might be different everytime

Any help will be greatly appreciated

Regard
CJ Smit
 
A

Anatoly

((BoundColumn)DataGrid1.Columns[not edited column index]).ReadOnly = true;
HTH

CJ Smit said:
Hi Guys,

I have a datagrid that are databound at runtime and columns are added
automatically to it from the dataset. OnEdit Command renders all fields as
editable, my problem is I need to specify in some way which columns are not
editable, I have the columns I would like to lock for editing, but how do I
tell the datagrid this ? My columns are dynamically added to the grid at
runtime, because the dataset might be different everytime.
 
G

Guest

Thanks Anatoly

Do you perhaps have some example code snippet for me

Much appreciated

Regard
CJ Smit
 
S

Steven Cheng[MSFT]

Hi Smit,

Thanks for your followup. I've checked and found that the attachement seems
only avaliable in OE client. Would you please check the message in OE? Any
way, I've post the page's code here in case that you may got any
difficulties using the OE client:

-------------------------------aspx
page-------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>BindGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Operation" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

---------------------------------code behind page
class----------------------------------
public class BindGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Load_Data();
Bind_Data();

}
}

protected void Load_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("description");

for(int i=1;i<=10;i++)
{
DataRow row = tb.NewRow();
row["index"] = i.ToString();
row["name"] = "Name" + i.ToString();
row["description"] = "Description" + i.ToString();
tb.Rows.Add(row);
}

Session["TEMP_DATA"] = tb;
}

protected void Bind_Data()
{
DataTable tb = (DataTable)Session["TEMP_DATA"];
dgMain.DataSource = tb;
dgMain.DataBind();
((BoundColumn)dgMain.Columns[0]).ReadOnly = true;
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.dgMain.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_CancelComm
and);
this.dgMain.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_EditComman
d);
this.dgMain.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = e.Item.ItemIndex;
Bind_Data();
}
}

================================================

Please check it out and feel free to let me know if you still have any
problems on it.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top