I have 2 GridView grdvieMarks for add online questionAnssheet and GridviewMaxmarks for adding maxmarks for each question.
I know only first 2 columns of gridviewmarks, id and name of each row, and other columns count dynamic and these 2 columns are also dynamic. On save button i want to find each id, admission number and max marks of that question
I get data in dataset, only its 2 column (id and emplyeename) name are same, otherwise all dynamic (column name questionno, and value is its answer).
I firstly bind it in gridviewmarks, and then i create another GridView to enter maxmarks of each question.
On save i want to find id, questionno, ans(value of each question), maxmarks entered.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="grdvieMarks" runat="server"></asp:GridView>
<div id="dimxmatks" style="overflow: auto">
<asp:GridView ID="GridviewMaxmarks" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridviewMaxmarks_RowDataBound"></asp:GridView>
</div>
<asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnsave" />
</Triggers>
</asp:UpdatePanel>
rdvieMarks.DataSource = dsCustomers;
grdvieMarks.DataBind();
{
GridviewMaxmarks.Columns.Clear();
DataTable dt = new DataTable();
int cols = Convert.ToInt32(dsCustomers.Tables[0].Columns.Count - 2);
int rows = Convert.ToInt32(dsCustomers.Tables[0].Rows.Count);
for (int i = 0; i < cols; i++)
{
TemplateField field = new TemplateField();
field.HeaderText = dsCustomers.Tables[0].Columns[i + 2].ColumnName.ToString();
GridviewMaxmarks.Columns.Add(field);
}
if (rows > 0)
{
dt.Rows.Add();
}
GridviewMaxmarks.DataSource = dt;
GridviewMaxmarks.DataBind();
DataTable dt2 = new DataTable();
{
for (int i = 0; i < GridviewMaxmarks.Columns.Count; i++)
{
dt.Columns.Add("column" + i.ToString());
}
foreach (GridViewRow row in GridviewMaxmarks.Rows)
{
DataRow dr2 = dt.NewRow();
for (int j = 0; j < GridviewMaxmarks.Columns.Count; j++)
{
dr2["column" + j.ToString()] = row.Cells[j].Text;
}
dt.Rows.Add(dr2);
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdvieMarks.Rows.Count; i++)
{
clsQuestionAnsSheet obj = new clsQuestionAnsSheet();
obj.AdmissionNo = grdvieMarks.Rows[i][0].Text;
for (int j = 2; j < grdvieMarks.Columns.Count; j++)
{
string columnname = grdvieMarks.Columns[j].ToString();
obj.Subject_id = "1";
obj.Session = Session["2021-2022"].ToString();
obj.UserId = Session["id"].ToString();
obj.QuestionNo = columnname;
}
}
}
protected void GridviewMaxmarks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridviewMaxmarks.Columns.Count; i++)
{
int rowID = e.Row.RowIndex;
TextBox textbox = new TextBox();
//textbox.CssClass = "form-control input-sm";
textbox.ID = GridviewMaxmarks.Columns[i].ToString();
RegularExpressionValidator regex = new RegularExpressionValidator();
regex.ID = "Regex" + GridviewMaxmarks.Columns[i].ToString();
regex.ControlToValidate = textbox.ID;
regex.Display = ValidatorDisplay.Dynamic;
regex.SetFocusOnError = true;
regex.ValidationExpression = "^([0-9]\\d*)(\\.\\d{1,2})?$";
regex.ErrorMessage = "INvalid Value";
e.Row.Cells[i].Controls.Add(textbox);
e.Row.Cells[i].Controls.Add(regex);
}
}
}