Reputation: 85
I want to how to add data into datatable and change dataformatString of "Date" column
Q1. how to change data format to "dd/MM/yyyy" or use your method to show,
Q2. I've add codes for add datakeys:["RowID"] in page_load event, but when i click "Edit" button, it prompts "object reference not set to an instance of an object", how can get datakey value/ add commandArguement to "edit"/"delete" Button
Q3. when i click "Edit" button, how to change into edit mode with update, cancel button etc for editing,
I hope someone can help me, thank you very much!!!
remark:"Edit & Delete Button are added in .aspx, not dynamically "
.cs
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack) {
GridView gv = (GridView)Page.FindControl("GridView1");
gv.DataKeyNames = new string[] { "RowID" };
}
}
protected void btn_Click(object sender, EventArgs e){
getDT(date); //string []date;
}
private DataTable getDT(string[] date){
DataTable dt = new DataTable();
dt.Columns.Add("RowID", typeof(Int16));
dt.Columns.Add("Date", typeof(DateTime));
for (int i = 0; i < date.Length; i++) {
dt.Rows.Add(i + 1, date[i]); //date[i] format:yyyy-mm-dd or dd/MM/yyyy
}
return dt;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Delete"){
hf_id.Value = GridView1.DataKeys[0].Value.ToString();
}
}
Upvotes: 1
Views: 2758
Reputation: 1
this is correct..but i want to be this without using data base..i did everything..now i want if i click the cell so data is updating in a gv but it is not showing datatable..so i want to take a refresh button.. if i click on this so data should be update on a data table.. i am sending the code like this...
design page
code---
protected void Page_Load(object sender, EventArgs e){
if (!this.IsPostBack) {
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("City") });
dt.Rows.Add(1, "Anamika", "Bangalore");
dt.Rows.Add(2, "Sunny", "Chennai");
dt.Rows.Add(3, "Monika", "Bangalore");
dt.Rows.Add(4, "Jyoti", "Chennai");
dt.Rows.Add(5, "Radhika", "Jabalpur");
dt.Rows.Add(6, "Imran", "Jammu");
dt.Rows.Add(7, "Alok", "Delhi");
dt.Rows.Add(8, "Amit", "Shamshabad");
dt.Rows.Add(9, "Neetu", "Bhopal");
dt.Rows.Add(10, "Jyoti", "Chennai");
dt.Rows.Add(11, "Radhika", "Vidisha");
dt.Rows.Add(10, "Pooja", "Pune");
gridview1.DataSource = dt;
gridview1.DataBind();
}
}
protected void gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
for (int i = 0; i < e.Row.Cells.Count; i++) {
TextBox txt = new TextBox();
txt.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = "";
e.Row.Cells[i].Controls.Add(txt);
}
}
}
}
Upvotes: 0
Reputation: 309
Download source code from given link and study it will help you. Your requirements are easy but basic knowledge is necessary for doing it.
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET
Upvotes: 2