RSS

DataTable Create programmatically And Save Data pass to load gridview

11 Aug

Purpose: Why we Need you Share this Article…..

because we Need First Save data before database ,i can manipulate after accurate then click on save and save in db.

Some Text box and other control use and Add Button… After Click on Add Button the value of control save in datatable and datatable pass to gridview.the gridview load the data.after add maximum record then i click on final Save Button

the Gridview Load Data Save in Database.

.NET 2.0 and 3.0  it is possible to create a DataTable without creating a DataSet.you just  need  create the DataTable object:

Process 1:

First Create the Datatable Object with specific Name

DataTable dataTable = new DataTable(“Routine”);

Process 2:

Create a Function of make a Table

public void AddRoutine() {

DataColumn col0 = new DataColumn(“RoutineID”, typeof(int));
col0.ReadOnly = true;
col0.Unique = true;
col0.AutoIncrement = true;
DataColumn col1 = new DataColumn(“SubjectTitle”, typeof(string));
DataColumn col2 = new DataColumn(“Days”, typeof(string));
DataColumn col3 = new DataColumn(“LectureDate”, typeof(string));
DataColumn col4 = new DataColumn(“StartTime”, typeof(string));
DataColumn col5 = new DataColumn(“EndTime”, typeof(string));
DataColumn col6 = new DataColumn(“ClassType”, typeof(string));
DataColumn col7 = new DataColumn(“RoomFloor”, typeof(string));
DataColumn col8 = new DataColumn(“RoomNo”, typeof(string));
DataColumn col9 = new DataColumn(“RoomTitle”, typeof(string));
DataColumn col10 = new DataColumn(“Teacher”, typeof(string));

dataTable.Columns.AddRange(new DataColumn[] { col0, col1, col2, col3, col4, col5, col6, col7, col8, col9, col10 });
Session[“Routine”] = dataTable;

}

Process 3:

create a button and write this on click Event.After Enter the record in Control and click button load data in gridview

protected void btnLoadGV_Click(object sender, EventArgs e)
{
//Create Routine Table
AddRoutine();
DataRow row;
DataTable dt = (DataTable)Session[“Routine”];

row = dt.NewRow();
txtRoomNo.Text = string.Empty;
txtRoomFloor.Text = string.Empty;
txtRoomTitle.Text = string.Empty;

row[“SubjectTitle”] = txtSubjTitle.Text;
row[“Days”] = ddlDays.SelectedItem.Text;
row[“LectureDate”] = txtLectureDate.Text;
row[“StartTime”] = txtStartTime.Text;
row[“EndTime”] = txtEndTime.Text;
row[“ClassType”] = ddlClassType.SelectedItem.Text;
row[“RoomFloor”] = txtRoomFloor.Text;
row[“RoomNo”] = txtRoomNo.Text;
row[“RoomTitle”] = txtRoomTitle.Text;
row[“Teacher”] = ddlTeacher.SelectedItem.Text;
dt.Rows.Add(row);
Session[“Routine”] = dt;

gvclassdetail.DataSource = dt;
gvclassdetail.DataBind();

}

Process 4

Load this DAta in Gridview

<asp:GridView ID=”gvclassdetail” runat=”server” AutoGenerateColumns=”False”
CellSpacing=”5″ CellPadding=”2″ DataKeyNames=”RoutineID”
onrowdeleting=”gvclassdetail_RowDeleting”>
<Columns>

<asp:CommandField ShowDeleteButton=”True” />

<asp:BoundField DataField=”RoutineID” HeaderText=”RoutineID” />
<asp:BoundField DataField=”SubjectTitle” HeaderText=”Subject Title” />
<asp:BoundField DataField=”LectureDate” HeaderText=”Date”  />
<asp:BoundField DataField=”StartTime” HeaderText=”Start Time” />
<asp:BoundField DataField=”EndTime” HeaderText=”End Time” />
<asp:BoundField DataField=”ClassType” HeaderText=”ClassType” />
<asp:BoundField DataField=”RoomFloor” HeaderText=”RoomFloor” />
<asp:BoundField DataField=”RoomNo” HeaderText=”RoomNo” />
<asp:BoundField DataField=”RoomTitle” HeaderText=”RoomTitle” />
<asp:BoundField DataField=”Teacher” HeaderText=”Teacher” />

</Columns>

<EmptyDataTemplate>
<asp:Label ID=”lblEmptyGV” runat=”server” Font-Bold=”True” ForeColor=”White”
Text=”No Records Found”></asp:Label>
</EmptyDataTemplate>

</asp:GridView>

Process 5

For Delete the Gridview Record from Datatable

protected void gvclassdetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

DataTable del = (DataTable)Session[“Routine”];
del.Rows[e.RowIndex].Delete();
Session[“Routine”]=del;

gvclassdetail.DataSource = del;
gvclassdetail.DataBind();

}

Process 6:

After Load the Gridview then you Want to All Data of Gridview Save in Database then Write this Code at Second

Final SAve Button

DataTable dts = (DataTable)Session[“Routine”];
int restult=-1;
foreach (DataRow dataRow in dts.Rows)
{
DALRoutineManagement Routinemgn = new DALRoutineManagement();
Routinemgn.RoutineID = _Res;

Routinemgn.SubjectTitle = dataRow[“SubjectTitle”].ToString();
Routinemgn.Days = dataRow[“Days”].ToString();
Routinemgn.LectureDate = Convert.ToDateTime(dataRow[“LectureDate”].ToString());
Routinemgn.StartTime = dataRow[“StartTime”].ToString();
Routinemgn.EndTime = dataRow[“EndTime”].ToString();
Routinemgn.ClassType = dataRow[“ClassType”].ToString();
Routinemgn.RoomFloor = dataRow[“RoomFloor”].ToString();
Routinemgn.RoomNo = Convert.ToInt32(dataRow[“RoomNo”].ToString());
Routinemgn.RoomTitle = dataRow[“RoomTitle”].ToString();
Routinemgn.TeacherName = dataRow[“Teacher”].ToString();
restult = DALRoutineManagement.InsertUpdateRoutineDetail(Routinemgn);

}
if (restult > 0)
{
btnOk.Visible = true;
lblmsg.Visible = true;

}

End Tutorial

if solve your problem ,this kindly comments about this…or you need or any problem …send response me.

sajjad_mcs_iub@yahoo.com

Advertisements
 
Leave a comment

Posted by on August 11, 2010 in ASP Dot Net C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: