2014年7月13日 星期日

[RESOLVED] Insert Data From GridView to DB based on selected column and row


Hye I have extract data from excel sheet into DataTable and then bind it to gridview. The problem i only want to insert the wanted data from gridview into database. Refer image below


Example




For example i only want the data on the row and column that the checkbox is ticked to insert into database. So is that possible or is anyone have other suggestion to solve this problem..






urenjoy



refer following thread:


http://stackoverflow.com/questions/16801493/insert-data-of-selected-gridview-checkboxes-values-using-c-sharp-asp-net





Thanks for the reply urenjoy but the thread is only about how to selected item based on selected row only but i want to retrieve the data based on selected row and also the column..It is like array but i did not know how to get the data based on row and
column. Do you have any better idea?



hi,


You can define head template with a checkbox and in code behind to check row checkbox and column box.


such as :





ForeColor="White" />

runat="server" />





ForeColor="White" />




Text="StudentName"
runat="server" />



ForeColor="White" />




Text="Score"
runat="server" />





 protected void Button6_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Score", typeof(int));

foreach (GridViewRow c in GridView2.Rows)
{
CheckBox ckRow = (CheckBox)c.FindControl("CKSelect");//find row checkbox
if (ckRow.Checked)
{
foreach (DataControlField d in GridView2.Columns)
{
DataRow dr = dt.NewRow();

CheckBox ckColName = (CheckBox)GridView2.HeaderRow.FindControl("ShowName");// find column checkbox
CheckBox ckColScore = (CheckBox)GridView2.HeaderRow.FindControl("ShowScore");
Label lbName = (Label)c.FindControl("lbName");
Label lbScore = (Label)c.FindControl("lbScore");
if (ckColName.Checked)
{
dr["Name"] = lbName.Text;
}
if (ckColScore.Checked)
{
dr["Score"] = lbScore.Text;
}
dt.Rows.Add(dr);
}
}
}
}

Hope this helps





沒有留言:

張貼留言