How to display a data to textbox after selecting it from a dropdown list in a datagridview

Posted by Rabbil under Others on 12/28/2012 | Points: 10 | Views : 4526 | Status : [Member] | Replies : 15
Hi,
I am new to .net windows application and I need a help.

I've taken a DataGridView in my windows application and there are 5 columns out of which one is dropdownlist from which I've to select a value and after selecting a value its subsequent data must be displayed in the same row i.e in the textbox.Also user can edit the data available in the textbox (of the remaining 4 columns).And, dynamically the next row must be created for the same purpose.
I am getting the values in dropdown list of first row and dynamically next row is also created but not getting the data filled in the textboxes, neither I'm getting values for the next row in the dropdown list, rather I'm getting an exception "Input string not in correct format."

Please check the following code


 private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridViewComboBoxColumn dgvComboBox;
DataGridViewTextBoxColumn dgTxtBox;

dgvc = (DataGridViewComboBoxColumn)dataGridInvoice.Columns["dgvProductCode"];
int indexRow = dgvc.Index;
dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvProductName"];
dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvPriceColumn"];
dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvQuantityColumn"];
//dgvc = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvTotalColumn"];

string strText = dgvc.Selected.ToString();
string strText1 = dgvc.ValueMember.ToString();
string SelectedText = Convert.ToString((dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"] as DataGridViewComboBoxCell).FormattedValue.ToString());

int SelectedVal = Convert.ToInt32(dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"].Value); //Format Exception

string SelectedText1 = Convert.ToString((dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal1 = Convert.ToInt32(dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"].Value);

///
///Summary
///get the id from strText1 and pass it to the procedure along with command type 'Id' to fetch data of name and price of product

DataTable dt_ProductCategory = b.BindInvoiceDataGridView(strText1, "Id");
DataGridViewTextBoxColumn dgTxtProductName = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvProductName"];
//dgTxtProductName.DataSource = dt_ProductCategory
}

public DataTable BindInvoiceDataGridView(string id,string commandtype)
{
con.Open();
cmd = new SqlCommand("sp_InvoiceDataGridViewDisplay",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@prodId", id);
cmd.Parameters.AddWithValue("@commandType", commandtype);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
DataTable dtInvoiceDataGridViewDisplay = new DataTable();
sqlDataAdapter.Fill(dsInvoiceDataGridViewDisplay);
con.Close();
return dtInvoiceDataGridViewDisplay;
}

How to display the data in the row i.e in the text box and for the next row in the drop down list where am I going wrong ( where to make changes) ?

Please Help me !!

Thanks And Regards,
Rabbil Ahmad


"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards



Responses

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
bro have a look on my code it is working fine actualy m also fresher....
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{
dbc.myconnection();
GridView1.EditIndex = e.NewEditIndex;
fillgrid();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dbc.myconnection();
GridView1.EditIndex = -1;
fillgrid();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
dbc.myconnection();
if (e.CommandName.Equals("insert"))
{
TextBox txt1 = (TextBox)GridView1.FooterRow.FindControl("txtn");
TextBox txt2 = (TextBox)GridView1.FooterRow.FindControl("txtd");
SqlCommand cmd = new SqlCommand("insert into dmlwithingrid(name, decription)values('"+txt1.Text +"', '"+txt2.Text +"')", dbc .sqlcon );
cmd.ExecuteNonQuery();
fillgrid();

}

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dbc.myconnection();
int id = Convert.ToInt32(GridView1 .DataKeys [e.RowIndex ].Values["id"].ToString ());
SqlCommand cmd = new SqlCommand("delete from dmlwithingrid where id="+id, dbc.sqlcon );
cmd.ExecuteNonQuery();
fillgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dbc.myconnection();
int id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex ].Value.ToString());
string name = GridView1.DataKeys[e.RowIndex].Values["name"].ToString();
TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcity");
SqlCommand cmd = new SqlCommand("update dmlwithingrid set decription='" + txtcity.Text + "' where id=" + id, dbc.sqlcon);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
fillgrid();
}
}


Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
.aspx page i used template field way to make rows of the gridview.....
 <div> <asp:GridView ID="GridView1" DataKeyNames="id, name" runat="server"

AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="184px" Width="489px">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
&nbsp;<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" Height="40px"
ImageUrl="~/images/updateimage.jpg" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" Height="20px"
ImageUrl="~/images/updateimage.jpg" ToolTip="cancel" />&nbsp;
</EditItemTemplate>
<ItemTemplate>
&nbsp;<asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" Height="20px"
ImageUrl="~/images\editimagebutton.png"
ToolTip="edit" />&nbsp;
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" Height="30px"
ImageUrl="~/images/deletebuttonimage.jpg" ToolTip="delete"/>
</ItemTemplate>
<FooterTemplate>
&nbsp;<asp:ImageButton ID="imgbtnAdd" runat="server" CommandName="insert" Height="20px"
ImageUrl="~/images/addimagebutton.jpg" ToolTip="insert" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("name") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("name") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtn" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="description">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("decription") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("decription") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtd" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
</asp:GridView>
&nbsp;&nbsp;<br />
&nbsp;

</div>
</asp:Content>


Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
actually in my code i just only used textboxes in gridview......u can copy nd paste my .aspx page and can see it design and can change according to ur need....

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
actually in my code i just only used textboxes in gridview......u can copy nd paste my .aspx page and can see it design and can change according to ur need....

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks for your valuable response .
Yeah its working fine for web app.
But my requirement is in windows Application. Therefore, many properties and events differ in web and windows forms.
So, if possible can you experiment it with Windows as I'm not able to get which properties to use in Windows to meet these requirements.

Thanks & Regards,
Rabbil

"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
ohhk....actualy bro till now m just try to make web apps but keep doing nd google it definitely someone will help u....

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/28/2012 [Member] Bronze | Points: 25

Up
0
Down
@Ankitsrist i dont know why you have been posting aswers twice in almost every forum question.

Thanks
Pavan Kumar
Mark Answer if this fits the need

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
actualy bymistake it happens....sometimes it happened with my post also, generaly when i answer nd click on the submit button it keep on waiting to send reply so i canceled it thinking tht it is not going to publish so i agian click on submit button the next time it displays twice....

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down

I've googled a lot since last 4 days and got stuck at this stage.Finally, I had to post this question, as whatever I was getting was in web app.
Anyway thanks for your effort.



"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
nd there is no such option to delete the answer......

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
yeah rabbil most of content available on net is for web appli...pavanandey(the person who has commented above) sir can help u he helped me a lot.....

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
@Pavanandey Sir, can you help me in meeting this requirement (If Possible) ?


"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/28/2012 [Member] Bronze | Points: 25

Up
0
Down
:) @Rabbil i am working on web apps only.

Thanks
Pavan Kumar
Mark Answer if this fits the need

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rabbil on: 12/28/2012 [Member] Starter | Points: 25

Up
0
Down
Oh k.!! no issue, my next project is on web apps .
Thanks.
Rabbil


"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/28/2012 [Member] Bronze | Points: 25

Up
0
Down
hmm

Thanks
Pavan Kumar
Mark Answer if this fits the need

Rabbil, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response