Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid 's ItemTemplate and edit ItemTemplate

Status
Not open for further replies.

crxcte1

Programmer
May 22, 2003
74
US
I get no errors on the code below. When I click on the edit button in the datagrid the delete, cancel, update links are not displayed. Only the Edit button is displayed. This thing is killing me, I'll try any thing.

void Page_Load(Object sender, EventArgs e)
{
const string strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\ASPdotnet\\MembersList\\Database\\members.mdb";
OleDbConnection objConn = new OleDbConnection(strConnString);
objConn.Open();
const string strSQL = "SELECT * FROM members";
OleDbCommand objCmd = new OleDbCommand(strSQL, objConn);
OleDbDataReader objDR = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
datagrid1.DataSource = objDR;
datagrid1.DataBind();
}
void setEditMode(Object s,DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex= e.Item.ItemIndex;
bindDataGrid();
}
void bindDataGrid()
{
const string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\ASPdotnet\\MembersList\\Database\\members.mdb";
OleDbConnection myConn = new OleDbConnection(strConn);
string sqlStr="SELECT * From members WHERE id > 0 ORDER BY Lname";
myConn.Open();
OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter(sqlStr,myConn);
DataSet myDataSet = new DataSet();
myOleDbAdapter.Fill(myDataSet,"dtMembers");
datagrid1.DataSource=myDataSet.Tables["dtMembers"];
datagrid1.DataBind();
myConn.Close();
}
void DeleteMode(Object s,DataGridCommandEventArgs e)
{
const string strConnString ="Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\ASPdotnet\\MembersList\\Database\\members.mdb";
OleDbConnection objConn = new OleDbConnection(strConnString);
int intID;
intID = (int)datagrid1.DataKeys[e.Item.ItemIndex];
string strSQL = "DELETE FROM members WHERE ID='" + intID + "'";
objConn.Open();
try
{
OleDbCommand cmd = new OleDbCommand(strSQL, objConn);
cmd.ExecuteNonQuery();
}
catch (Exception ex1){
//Message.Style("color") = "red";
}
objConn.Close();
datagrid1.EditItemIndex=-1;
datagrid1.DataBind();
}
void cancelEdit(Object s,DataGridCommandEventArgs e){
datagrid1.EditItemIndex=-1;
datagrid1.DataBind();
}
void updateDataBase(Object s,DataGridCommandEventArgs e){
int intPid=(int)datagrid1.DataKeys[e.Item.ItemIndex];
TextBox strDate = (TextBox) e.Item.Cells[18].Controls[0];e.Item.Cells[1].Controls[0];Textbox)).Text,"'","''");

string strSQL="UPDATE members Set birthdate5='" + strDate + "' WHERE ID=' + intPid.ToString +'";
const string connStr="Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\ASPdotnet\\MembersList\\Database\\members.mdb";
OleDbConnection myConn = new OleDbConnection(connStr);
myConn.Open();
OleDbCommand myUpdateCommand = new OleDbCommand(strSQL, myConn);
myUpdateCommand.ExecuteNonQuery();
myConn.Close();
datagrid1.EditItemIndex=-1;
datagrid1.DataBind();
}

<FORM id="form1" runat="server">
<asp:datagrid id="datagrid1" runat="server" OnDeleteCommand="DeleteMode" OnUpdateCommand="updateDataBase"
OnCancelCommand="cancelEdit" OnEditCommand="setEditMode" GridLines="Both" AutoGenerateColumns="false"
HeaderStyle-BackColor="Gray" Font-Size="8" HeaderStyle-Font-Size="8" HeaderStyle-Font-Bold="True"
CellPadding="3" BorderWidth="0" Font-Name="Verdana" BackColor="Navy" ForeColor="White" datakeyfield="ID">
<Columns>
<asp:templatecolumn HeaderStyle-BackColor="#ffff33" HeaderStyle-ForeColor="#FFFFFF" ItemStyle-Width="50"
HeaderStyle-Height="20px">
<ItemTemplate>
<asp:button CommandName="Edit" Text="Edit" runat="server" ID="Button1" />
</ItemTemplate>
<EditItemTemplate>
<asp:linkbutton CommandName="Update" Text="Update" runat="server" Width="50" ID="UpdateButton" Height="18px"
Font-Size="8" />
<asp:linkbutton CommandName="Delete" Text="Delete" runat="server" Width="50" ID="DeleteButton" Height="18px"
Font-Size="8" />
<asp:linkbutton CommandName="Cancel" Text="Cancel" runat="server" Width="50" ID="CancelButton" Height="18px"
Font-Size="8" />
</EditItemTemplate>
</asp:templatecolumn>
<asp:BoundColumn HeaderText="ID" datafield="ID" />
<asp:BoundColumn HeaderText="Fname1" datafield="Fname1" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="Lname" datafield="Lname" ItemStyle-Wrap="false" />
<asp:BoundColumn HeaderText="Birthdate1" datafield="Birthdate1" DataFormatString="{0:d}" ItemStyle-Wrap="false" />
</Columns>
</asp:datagrid></FORM>
 
Try something more like this in your EditItemTemplate:
Code:
<EditItemTemplate>
<asp:TextBox ID="txtID" Text='<%# DataBinder.Eval(Container.DataItem,"ID")%>' />
<asp:TextBox ID="txtfName" Text='<%# DataBinder.Eval(Container.DataItem,"fName")%>' />
<asp:TextBox ID="txtLName" Text='<%# DataBinder.Eval(Container.DataItem,"LName")%>' />
<asp:TextBox ID="txtBirthDate1" Text='<%# DataBinder.Eval(Container.DataItem,"BirthDate1")%>' />
</EditItemTemplate>
 
Thanks for the input Veep. When I click on the edit button the linkbuttons on update, cancel, and delete should appear in the datagrid. I tried adding your code but had no change, the edit button displays over again with no errors.

ctr
 
Could you recommend a source for using the wizard. I've been writing code for C# and never used the wizard. Books or web site?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top